Delayed Execution of Javascript

6 replies
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************************************** *';
var exitsplashmessage = '***************************************\n\n W A I T B E F O R E Y O U G O !\n\n CLICK THE *CANCEL* BUTTON RIGHT NOW\n TO STAY ON THE CURRENT PAGE.\n\n I HAVE SOMETHING VERY SPECIAL FOR YOU!\n\n***************************************';
var exitsplashpage = 'http://www.redirectpage.com';
</script>
<script language="javascript" src="http://www.mydomain.com/exitsplash.php"></script>
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
#delayed #execution #javascript
  • Profile picture of the author lordspace
    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() }
    Signature

    Are you using WordPress? Have you tried qSandbox yet?

    {{ DiscussionBoard.errors[6972567].message }}
    • Profile picture of the author eswariseo
      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">
      {{ DiscussionBoard.errors[6974650].message }}
      • Profile picture of the author Ed Micah
        Originally Posted by eswariseo View Post

        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">
        No, I want the download to start immediately when they land it on that page. And an exit splash page pops up when they try to leave.
        {{ DiscussionBoard.errors[6975957].message }}
  • Profile picture of the author Dan Grossman
    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.
    Signature
    Improvely: Built to track, test and optimize your marketing.

    {{ DiscussionBoard.errors[6979888].message }}
  • Profile picture of the author John Ayling
    The jQuery way of binding to the unload event is like this
    Code:
    $(window).unload(function() {   //launch your popup here });
    Signature
    Software Marketing & Licensing System for WordPress Plugins, Themes & .NET Software
    >> 72 Hour Special <<
    {{ DiscussionBoard.errors[6981358].message }}
  • Profile picture of the author andy789
    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
    {{ DiscussionBoard.errors[6981711].message }}

Trending Topics