允许重新加载,但不允许使用javascript/jquery关闭页面 [英] allow reload but not page closing using javascript/jquery

查看:108
本文介绍了允许重新加载,但不允许使用javascript/jquery关闭页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户尝试关闭网页时向用户发出警告消息.以下代码可以正常工作,但是我想每60秒刷新一次页面,并且此代码不允许自动刷新.它发出警告刷新消息.但是我只想在用户单击关闭选项卡

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,如下图所示.

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>

在60000毫秒(即1分钟)后调用设置间隔的脚本,这使您可以先设置hook = false然后刷新.

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

这篇关于允许重新加载,但不允许使用javascript/jquery关闭页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆