You may need organize your scripts and assets, but if you absolutely need it in the head, you can put it on the mobileinit
event before loading jQuery Mobile:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function() { $("#events").append("jQuery Dom Ready<br>"); });
$(document).on('mobileinit', function () {
function showme(e, ui){
var activePage = $(":mobile-pagecontainer").pagecontainer("getActivePage").prop("id");
$("#events").append("JQM Event: "+e.type+": "+activePage+"<br>");
if(activePage) {
$("#events").append("JQM Active Page: "+activePage+"<br>");
}
if(e.target){
if(e.target.id)
$("#events").append("Target Page: "+e.target.id+"<br>")
}
if(ui){
var to = ui.toPage;
if(ui.prevPage) {
var fromId = ui.prevPage.prop("id");
if (typeof fromId == "undefined") {
$("#events").append("JQM "+e.type+" from -initialization-<br>");
} else {
$("#events").append("JQM "+e.type+" from: " + fromId+"<br>");
}
}
if (typeof ui.toPage == "object") {
$("#events").append("Can manipulate " + ui.toPage.prop("id")+"<br>");
}
}
}
$(document).on("pagecreate", function(e, ui) { showme(e, ui); });
$(document).on("pagecontainertransition", function(e, ui) { showme(e, ui); });
$(document).on("pagecontainerbeforechange", function(e, ui) { showme(e, ui); });
$(document).on("pagecontainerbeforeshow", function(e, ui) { showme(e, ui); });
$(document).on("pagecontainershow", function(e, ui) { showme(e, ui); });
$(document).on("pagecontainerhide", function(e, ui) { showme(e, ui); });
});
</script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="page-1">
<div role="main" class="ui-content">
<div id="events">
</div>
</div>
</div>
</body>
</html>
Finally, as you complain the lack of documentation, i am giving you some useful links:
If you need some more details about $( document ).ready
, here is a great answer from Gajotres updated for the current jQuery Mobile 1.4.5 version:
jQuery Mobile: document ready vs page events
...and here you can find a wonderful graph explaining all the details about JQM page navigation:
jQuery Mobile “Page” Events – What, Why, Where, When & How? from Omar
Finally, here you can find the JQM playground:
Navigate between pages and open and close panel and popup widgets to see which events fire and their data.