Javascript window.onbeforeunload在第二页中不工作
您好,我的移动应用程序中有两个html页面,如下所示:Javascript window.onbeforeunload在第二页中不工作,javascript,jquery,jquery-ui,cordova,jquery-mobile,Javascript,Jquery,Jquery Ui,Cordova,Jquery Mobile,您好,我的移动应用程序中有两个html页面,如下所示: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery Mobile: Demos and Documentation</title> <
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile: Demos and Documentation</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/assets/css/jqm-docs.css" />
<link rel="stylesheet" href="docsdemos-style-override.css" />
<script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" src="jquery.mobile/jquery.mobile-1.1.0.js"></script>
<!-- Uncomment following line to access PhoneGap APIs (not necessary to use PhoneGap to package web app) -->
<!-- <script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>-->
</head>
<body>
<div data-role="page" id="jqm-home" class="type-home">
<div data-role="content">
<a href="example.html" data-role="button" id="myButton">Index</a>
</div>
</div>
</body>
</html>
jQuery Mobile:演示和文档
我的example.html如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile: Demos and Documentation</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/assets/css/jqm-docs.css" />
<link rel="stylesheet" href="docsdemos-style-override.css" />
<script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" src="jquery.mobile/jquery.mobile-1.1.0.js"></script>
<!-- Uncomment following line to access PhoneGap APIs (not necessary to use PhoneGap to package web app) -->
<!-- <script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>-->
</head>
<body>
<div data-role="page" id="jqm-home" class="type-home">
<div data-role="content">
<a href="example.html" data-role="button" id="myButton1">Test</a>
</div>
<script type="text/javascript">
window.onbeforeunload = function(evt) {
console.log ("*****************");
}
</script>
</div>
</body>
</html>
jQuery Mobile:演示和文档
window.onbeforeunload=函数(evt){
console.log(“*******************”);
}
现在假设点击索引按钮,我进入example.html页面。单击example.html中的“后退”按钮,它将再次转到index.html。一切正常,但它不打印console.log(“*********”;如果我在index.html上再按一次返回键,它就会打印出来,我想要的是当我在example.html上点击返回按钮时,它就会打印出来
上面的代码有什么问题?为什么会这样?如果您有任何建议,我们将不胜感激。您所能做的就是在“后退”按钮中添加一个事件 如果您有一个按钮:
<button id="backButton"> Go Back </button>
请记住,如果您单击一个按钮,将出现一篇文章,页面将刷新。因此,您应该在另一页的documentready中添加log函数
因此,如果加载example.html页面,您只需触发此事件
$(function(){
//All code that starts when the page is fully loaded.
console.log ("*****************");
});
在jquerymobile标准使用中,您在站点上的孔体验基本上停留在同一页面上。“更改”页面基本上会将内容动态添加到当前文档中,这意味着它不会触发您的
onbeforeunload
事件。
但是,您可以使用,具体使用哪一种取决于您想要采取什么样的行动。很可能您会看到pagebeforehide
或pagehide
$(function(){
//All code that starts when the page is fully loaded.
console.log ("*****************");
});