Delayed Execution of Javascript

by 6 replies
7
I'd like you guys to solve a coding situation here. And this is what I have at the moment:
So basically I have a download page which is installed with a exitsplash.php(a piece of code that pops up message, and redirection) and a meta redirect script that executes the file to be downloaded in 0 seconds when the user lands on the page.

The problem is that, when these 2 pieces of codes are installed at a page, when the download executes, the exit message pops up (which is not what I wanted). The exit message is suppose to pop up when the user leaves the page.

So what I can think of right now is to delay the exitsplash.php for 5 seconds or so? and when the user has downloaded the file(which the message won't pop up since it's disabled for 5 seconds), the exitsplash.php goes back normal.

Is it possible to do this?

Thanks
#programming #delayed #execution #javascript
  • I would do it differently.

    I would do this:
    remove the meta refresh
    put a hidden link to the download file with target "_blank" attribute
    then with javascript trigger the click to that link

    hope it works.

    another option with your approach could be to have a cookie sent from the download script.
    when the user tries to close the window i.e. the exit popup will check
    if (!download_initiated) { show_popup() }
    • [1] reply
    • You can use this script for delay redirection upto 5 secs
      <meta http-equiv="refresh" content="5;url=http://www.mydomain.com/download_file.zip">
      • [1] reply
  • If you want the message to appear when someone closes the page, you should be binding your function to the unload event of the document.
  • The jQuery way of binding to the unload event is like this
    Code:
    $(window).unload(function() {   //launch your popup here });
  • Try this ...
    HTML Code:
     <script language="javascript" type="text/javascript">     var exitsplashmessage = "Your message here";     var exitsplashpage = 'destinationurl . com/ pageexample /';  function addLoadEvent(func) {      var oldonload = window.onload;      if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); }  func();  } } }  function addClickEvent(a,i,func) {      if (typeof a[i].onclick != 'function') { a[i].onclick = func; }  }  var theDiv = '<div id="ExitSplashDiv"  style="display:block; width:100%; height:100%; position:absolute; background:#FFFFFF; margin-top:0px; margin-left:0px;" align="center">'; theDiv = theDiv + '<iframe src="'+exitsplashpage+'" width="100%" height="100%" align="middle" frameborder="0"></iframe>'; theDiv = theDiv + '</div>';  theBody = document.body;  if (!theBody) {theBody = document.getElementById("body");  if (!theBody) {theBody = document.getElementsByTagName("body")[0]; } } var PreventExitSplash = false;  function DisplayExitSplash(){      if(PreventExitSplash == false){          window.scrollTo(0,0);         PreventExitSplash=true;          divtag = document.createElement("div");          divtag.setAttribute("id","ExitSplashMainOuterLayer");          divtag.style.position="absolute";          divtag.style.width="100%";          divtag.style.height="100%";          divtag.style.zIndex="99";          divtag.style.left="0px";          divtag.style.top="0px";          divtag.innerHTML=theDiv;          theBody.innerHTML="";          theBody.topMargin="0px";          theBody.rightMargin="0px";          theBody.bottomMargin="0px";          theBody.leftMargin="0px";          theBody.style.overflow="hidden";          theBody.appendChild(divtag);      return exitsplashmessage;      } }  var a = document.getElementsByTagName('A');  for (var i = 0; i < a.length; i++) {      if(a[i].target !== '_blank') {addClickEvent(a,i, function(){ PreventExitSplash=true; });} else{addClickEvent(a,i, function(){ PreventExitSplash=false;});} }  disablelinksfunc = function(){     var a = document.getElementsByTagName('A');      for (var i = 0; i < a.length; i++) {          if(a[i].target !== '_blank') {addClickEvent(a,i, function(){ PreventExitSplash=true; });} else{addClickEvent(a,i, function(){ PreventExitSplash=false;});}     } }  hideexitcancelbuttonimage = function(){     document.getElementById('ExitCancelButtonImageDiv').style.display='none';  } addLoadEvent(disablelinksfunc);  disableformsfunc = function(){     var f = document.getElementsByTagName('FORM'); for (var i=0;i<f.length;i++){ if (!f[i].onclick){ f[i].onclick=function(){ PreventExitSplash=true; } }else if (!f[i].onsubmit){ f[i].onsubmit=function(){ PreventExitSplash=true;  }  }  addLoadEvent(disableformsfunc); window.onbeforeunload = DisplayExitSplash; </script>
    Reference... Google

Next Topics on Trending Feed

  • 7

    I'd like you guys to solve a coding situation here. And this is what I have at the moment: <meta http-equiv="refresh" content="0;url=http://www.mydomain.com/download_file.zip"> <script language="javascript"> var exitsplashalertmessage = '***************************************\n\n > > > W A I T < < <\n\n CLICK THE ***CANCEL*** BUTTON\n on the NEXT Window for Something\n VERY Special!\n\n************************************** *';