JQuery Mobile,通过window.location重定向可以防止pageshow事件 [英] JQuery Mobile, redirecting by window.location prevents pageshow event
问题描述
我有一个JQuery Mobile应用程序,其中有许多页面相互链接。
如果我在页面A上放置一个链接并将href设置为页面B,则此脚本可以正常工作:
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!');
});
但如果我在页面A上放一个按钮并为onclick事件编写此代码:
But if I put a button on page A and write this code for onclick event:
window.location="pageB.html";
页面展示活动不再举起!
问题出在哪里?我如何使用window.location并仍然可以捕获pageshow事件?
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?
推荐答案
当你使用 window.location
或 window.location.href
,您在没有Ajax的情况下正常加载页面,并删除DOM中的所有先前页面。
When you use window.location
or window.location.href
, you load pages normally without Ajax, and removes all previous pages in DOM.
如果在 pageA.html 中加载了JS库,则在加载 pageB.html 时它们将无法工作,因为它们已删除。
If you have JS libraries loaded in pageA.html, they won't work when you load pageB.html, because they are removed.
因此,您需要将JS库放在 pageB.html 的< head>
让JS代码/库工作。
Hence, you need to place JS libraries in pageB.html's <head>
to have JS code/libraries working.
使用
// jQM 1.3.2 and below
$.mobile.changePage("#page_id" or "URL");
// jQM 1.4
$.mobile.pageContainer.pagecontainer("change", "#page_id" or "URL");
这篇关于JQuery Mobile,通过window.location重定向可以防止pageshow事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!