在卸载功能之前,在窗口中单击“重新加载”按钮时,请调用JavaScript的功能



我在JavaScript中具有一个函数,该功能在单击关闭或刷新按钮时执行。

$(window).bind("beforeunload",function(event) {
            return "Save or Not";
    }
});

这将为我提供一个弹出窗口,当我单击"刷新"按钮(重新加载,不要在Chrome中重新加载(时,它会有2个按钮。现在,如果我单击"重新加载"按钮,我想调用函数重定向到我的主页。我怎样才能做到这一点?

请尝试此

  $(document).ready(function() {

      setTimeout(function() {
      var __redirect_to = 'url_where_to_send';//
      var _tags = ['button', 'input', 'a'], _els, _i, _i2;
      for(_i in _tags) {
          _els = document.getElementsByTagName(_tags[_i]);
          for(_i2 in _els) {
              if((_tags[_i] == 'input' && _els[_i2].type != 'button' && _els[_i2].type != 'submit' && _els[_i2].type != 'image') || _els[_i2].target == '_blank') continue;
              _els[_i2].onclick = function() {window.onbeforeunload = function(){};}
          }
     }
      window.onbeforeunload = function() {
          setTimeout(function() {
              window.onbeforeunload = function() {};
              setTimeout(function() {
                  document.location.href = __redirect_to;
              }, 500);
          },5);
          return 'Are you sure you want to leave this page?nnWait...Before you go:nn***************************************nn GET A FREE BOTTLE OF NeuroNZTnn***************************************nnTo receive your bottle FOR FREE - Click Cancel or Stay on Page below';
      }
      }, 500);
  });
  </script>

相关内容

最新更新