问题
I have a JQuery Mobile application with many pages linked to each other. If I put a link on page A and set the href to page B, this script works fine:
$(document).on('pageshow','#pageB',function(){
alert('hello!');
});
But if I put a button on page A and write this code for onclick event:
window.location="pageB.html";
The pageshow event won't raise anymore! Where's the problem? How can I use window.location and still be able to catch the pageshow event?
回答1:
When you use window.location
or window.location.href
, you load pages normally without Ajax, and removes all previous pages in DOM.
If you have JS libraries loaded in pageA.html, they won't work when you load pageB.html, because they are removed.
Hence, you need to place JS libraries in pageB.html's <head>
to have JS code/libraries working.
Update
Load pages / html files via Ajax using
// jQM 1.3.2 and below
$.mobile.changePage("#page_id" or "URL");
// jQM 1.4
$.mobile.pageContainer.pagecontainer("change", "#page_id" or "URL");
来源:https://stackoverflow.com/questions/20292693/jquery-mobile-redirecting-by-window-location-prevents-pageshow-event