简体   繁体   中英

allow reload but not page closing using javascript/jquery

i want to give warning message to users when they try to close the webpage.the below code is working just fine but i want to refresh the page after every 60 seconds and this code is not allowing to refresh automatically.it gives warning message for refresh as well. but i want to show warning message only for closing when users click on close tab

 <html>
  <head><meta http-equiv="refresh" content="60;url=test.html"/>
  <script type="text/javascript">
  var hook = true;
  window.onbeforeunload = function() {
    if (hook) {
      return "msg here"
    }
  }
  function unhook() {
    hook=false;
  }
   </script>
  </head>
  <body>

  </body>
 </html>

Your unhook method is not getting called anywhere. To do so you can refresh the page using

Javascript where you can call unhook also as shown velow.

<html>
<head>
  <script type="text/javascript">
    var hook = true;
    window.onbeforeunload = function() {
     if (hook) {
       return "msg here"
     }
    }
    function unhook() {
     hook=false;
    }
   setInterval(function(){
     unhook();
     window.location.reload()
   }, 60000);   
  </script>
 </head>
 <body>

 </body>
 </html>

The script in set interval is called after 60000 milli-seconds (ie 1 min) which allows you to first set hook=false and then refresh.

You can do it like this:

<script type="text/javascript">
   $(document).ready(function(){
     setTimeout(function(){
        window.location.reload();  // This will refresh the page
     }, 60000);   // 60 seconds
   });
</script>

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