JQuery Mobile, redirecting by window.location prevents pageshow event

南楼画角 提交于 2019-12-06 09:50:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!