【问题标题】:Javascript onbeforeunload to open window.open() popupJavascript onbeforeunload 打开 window.open() 弹出窗口
【发布时间】:2012-04-06 20:10:32
【问题描述】:

我正在尝试编写一个触发 window.open(url) 等的 onbeforeunload 事件。我希望在用户尝试离开页面或关闭浏览器时触发它,但在点击任何一个时不会触发页面上的按钮。页面上的按钮通过 javascript 将数据发布到同一页面。

javascript:

window.onbeforeunload = doSync;

function doSync(){
   if(doSync == true){
       //do sync via popup
       window.open("http://mydomain.com/page.php?var=<?php=sync_var?>", "Synchronizing cluster....", "location=0,menubar=0,statusbar=1,width=10,height=10");
   }
   else {
     //somehow do nothing and allow user to leave

   }
}
-->
</script>

按钮调用创建表单并提交表单的 javascript 函数。在那个 javascript 函数中,我设置了 doSync = false 的全局变量。我将包含这个函数的基本代码只是为了说明它。

function buttonPush(){
   var form = document.createElement('form');
   form.setAttribute('method' bla bla

   //before submit set dosync to false
   doSync = false;

   form.submit();
}

现在我在 window.onbeforeunload = doSync; 语句中收到 Not Implemented 错误。

任何帮助将不胜感激。

谢谢,

吉姆

我的 window.open 有问题吗?如果我做一个window.open('','','height=100,width=100');

它可以正常打开,但下面的却没有。

window.open('https://mydomain.com/support/sync_cluster.php?sync_cluster=mycluster','Synchronizing...', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=100,height=100');

【问题讨论】:

  • 互联网工程 7/8

标签: javascript popup onbeforeunload window.open


【解决方案1】:

doSync 是一个函数,而不是布尔值;只需创建一个变量并适当地设置它:

var sync = true;
window.onbeforeunload = doSync;

function doSync() {
  if (sync == true) {
    //do sync via popup
    window.open("http://mydomain.com/page.php?var=<?php=sync_var?>", "Synchronizing cluster....", "location=0,menubar=0,statusbar=1,width=10,height=10");
  }
  else {
    //somehow do nothing and allow user to leave
    return;
  }
}
function buttonPush(){
   var form = document.createElement('form');
   // form.setAttribute('method' bla bla

   //before submit set dosync to false
   sync = false;

   form.submit();
}

【讨论】:

    【解决方案2】:

    试试这个:

    var vals = 0;
    
    function displayMsg() {
      window.onbeforeunload = null;
      window.location = "https://www.google.com";
    }
    
    window.onbeforeunload = function evens(evt) {
      var message = 'Please Stay on this page and we will show you a secret text.';
      if (typeof evt == 'undefined') {
        evt = window.event;
      }
      timedCount();
      vals++;
      if (evt) {
        evt.returnValue = message;
        return message;
      }
      trace(evt);
    }
    
    
    function timedCount() {
      t = setTimeout("timedCount()", 500);
      if (vals > 0) {
        displayMsg();
        clearTimeout(t);
      }
    }
    $(document).ready(function() {
      $('a,input,button').attr('onClick', 'window.onbeforeunload = null;')
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <a href="https://en.wikipedia.org/wiki/Frame_(World_Wide_Web)">Leave</a>

    【讨论】:

    • 这似乎不再起作用plungjan.name/SO/unbefore
    • 是的,chrome 往往会经常更改此功能。他们发现有人在利用它并对其进行修补。通常以一种破坏性的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多