正确的做法是:
function pageEngine(event, ui)
{
// this is the page change engine. It makes sure the appropriate
// javascript files get loaded and the page init function gets called
console.log("pageEngine: " +ui.toPage[0].id);
// ui.toPage[0].id = the id in the data-role="page"
// <div id="debugPage" data-role="page" data-theme="b">
switch(ui.toPage[0].id)
{
case "homePage":
homePageReady(); // reinit the home page - i.e. update unreads
break;
case "mapPage":
$.getScript("js/map.js", function( data, textStatus, jqxhr ) {
onMapPageReady();
}).fail(function(xhr, textStatus, error) { jsLoadError(xhr, textStatus, error, "map.js"); });
break;
case "thirdPage":
// do nothing as this page requires no .js
break;
default:
console.log("page not found: " +ui.toPage[0].id);
}
}
pageEngine() 被一个绑定到 window.onload 的小函数调用,如下所示:
<script type="text/javascript">
window.onload = function()
{
console.log("window.onload: " +timeStamp());
document.addEventListener("deviceready", onDeviceReady, false);
$(document).on("pagecontainerbeforeshow", function( event, ui ) {
if(typeof ui.toPage == "undefined" || typeof ui.toPage[0] == "undefined")
{
// during startup and app init there are a lot of these
console.log("toPage[0] is undefined");
return;
}
else
{
console.log("pagecontainerbeforeshow: " +ui.toPage[0].id);
}
pageEngine(event, ui);
});
}
</script>
在加载 jquery 之后设置上述内容,但在加载 jquery.mobile 之前设置。