Making this exit pop JavaScript redirect immediately

by 6 replies
9
Hello, I have this exit pop redirect Javascript. It works perfectly, except I want the page to redirect immediately while the prompt for the user to stay on page or leave is still there.

Does that make sense?

The script currently doesn't redirect until the user chooses to stay on page. But I have had one before that redirects immediately, even while the prompt is up.

Here is the script below. If anyone can help I would really appreciate it. Thanks.

<script language="javascript">
(function() {
setTimeout(function() {
var __redirect_to = 'http://myurl.com/';//

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 '************************************** My Return Message ***************************';
}
}, 500);
})();
</script>
#programming #exit #immediately #javascript #making #pop #redirect
  • the settimeout() function you are using causes the delay because of the second argument you passed..decrease the 500 to 1 and it will work...besides, the script you wrote is redundant..try rewriting it in a simple way
    • [2] replies
    • Thanks for the response. But I actually didn't write the code, someone provided it to me without much instruction, and I am not really much of a coder.

      I did however find the below script, which appears to be a much simpler script and does redirect immediately as I want it.

      Code:
      <script type="text/javascript" language="javascript">  var areYouReallySure = false; var internalLink = false; function areYouSure() { if (!areYouReallySure && !internalLink) { areYouReallySure = true; location.href="REDIRECT URL HERE"  return "* Message Here*"; } } window.onbeforeunload = areYouSure; </script>
      But the problem is it pops up on any links that are clicked on my webpage rather than just when people exit off the page. After some research, I read that this code is missing something along the lines of:

      onClick="dontpop();"
      onSubmit="dontpop();"

      If you or anyone else that's reading think you might know how I might add these lines into the script I would really appreciate it. Thanks.
      • [1] reply
    • Check this ......Javascript Redirect

      Lee
      • [1] reply

Next Topics on Trending Feed