简体   繁体   中英

window.onbeforeunload not working in second page

Hi I have two html pages in my mobile application as follows:

<!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>

my example.html is like:

<!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>

Now suppose on click of Index button I goes to example.html page. on click of back button in example.html it again goes to index.html. everything is fine, but it does not print console.log (" * ** * **** "); If i press one more back on index.html then it prints it, what I want is it should print that on click of back button when I am on example.html.

Whats wrong in above code? and why it behave like this? Any suggestion will be appreciated thanks in advance.

In Jquery Mobile standard use you basically stay on the same page during your hole experience on the site. "Changing" page basically adds content dynamically to the current document, meaning it will not trigger your onbeforeunload event. You can however use jquery mobile events , which one depends on exactly what kind of action you want to take. Most probably you would be looking at pagebeforehide or pagehide

What you can do is add an event to the back button.

If you have a button :

<button id="backButton"> Go Back </button>

you can add via jquery following event

    $("#backButton).click(function(){
       console.log ("*****************");
   });

Keep in mind, if you click a button, a post will happen and the page will be refreshed. So you should add the log function inthe document ready of the other page.

So if the example.html page loads, you just fire this event

$(function(){
   //All code that starts when the page is fully loaded.
    console.log ("*****************");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM