最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript onbeforeunload to open window.open() popup - Stack Overflow

programmeradmin1浏览0评论

I am trying to write a onbeforeunload event that triggers a window.open(url) etc. I want it to be triggered if the user trys to leave the page or if they close their browser, but not when they click any of the buttons on the page. The buttons on the page post data to the same page via a javascript.

javascript:

window.onbeforeunload = doSync;

function doSync(){
   if(doSync == true){
       //do sync via popup
       window.open(".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>

The buttons calls a javascript function that creates a form and submits it. In that javascript function I set the global variable of doSync = false. I'll include the basic code of this function just to illustrate it.

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

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

   form.submit();
}

right now I am getting a Not Implemented error on the window.onbeforeunload = doSync; statement.

Any help would be appreciated.

Thanks,

Jim

Is there something wrong with my window.open? if i do a window.open('','','height=100,width=100');

it opens fine but this below does not.

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

I am trying to write a onbeforeunload event that triggers a window.open(url) etc. I want it to be triggered if the user trys to leave the page or if they close their browser, but not when they click any of the buttons on the page. The buttons on the page post data to the same page via a javascript.

javascript:

window.onbeforeunload = doSync;

function doSync(){
   if(doSync == true){
       //do sync via popup
       window.open("http://mydomain./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>

The buttons calls a javascript function that creates a form and submits it. In that javascript function I set the global variable of doSync = false. I'll include the basic code of this function just to illustrate it.

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

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

   form.submit();
}

right now I am getting a Not Implemented error on the window.onbeforeunload = doSync; statement.

Any help would be appreciated.

Thanks,

Jim

Is there something wrong with my window.open? if i do a window.open('','','height=100,width=100');

it opens fine but this below does not.

window.open('https://mydomain./support/sync_cluster.php?sync_cluster=mycluster','Synchronizing...', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=100,height=100');
Share Improve this question edited Apr 7, 2012 at 13:12 Jim asked Apr 6, 2012 at 20:10 JimJim 6236 gold badges16 silver badges25 bronze badges 1
  • Internet Engineering 7/8 – Jim Commented Apr 7, 2012 at 12:17
Add a ment  | 

2 Answers 2

Reset to default 4

doSync is a function, not a boolean; just create a variable and set it appropriately:

var sync = true;
window.onbeforeunload = doSync;

function doSync() {
  if (sync == true) {
    //do sync via popup
    window.open("http://mydomain./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();
}

Try this:

var vals = 0;

function displayMsg() {
  window.onbeforeunload = null;
  window.location = "https://www.google.";
}

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./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="https://en.wikipedia/wiki/Frame_(World_Wide_Web)">Leave</a>

发布评论

评论列表(0)

  1. 暂无评论