Exit page redirect

I am wanting to create an automatic redirect exit page to another of my websites when my visitors leave my main website.

Well, when soneone leaves your web-page the onUnload event would be triggered. If you add a code to open a popup into the <body> tag, the popup would be shown every time they leave your page.

<body onUnload="window.open('http://MySite.com/popup.html')">

This is a useless solution. The popup would be shown even they click a link to another page inside your site. We need to block the popup when they click a link or submit a form at your page.


Lets add a onClick code to a link like below:
<a onClick="window.exitBlock=true"
  href="http://MySite.com/page2.html">Link</a>

Since now we can check the window.exitBlock flag-variable before showing the popup:
onUnload="if(!window.exitBlock)window.open('.../popup.html')"

 
In order do not insert the window.exitBlock=true code into every link and every form at your every page manually you can use a simple JS-script to do this for you.

1) Download the script

Click this link below with the right mouse-button then select the “Save Target As…” item and save the exitpage.js file on your local PC.

Download: exitpage.js

Now upload it onto your site beside your webpages.
Insert the <script> tag with the script-file just before the </body> tag of every page where you’d like the exit page redirection.

You can copy the code from the box below and paste it into your page.

2) Insert an onUnload script

Insert your onUnload code with the window.exitBlock checking into your <body> tag. All the code should look like below:

<html>
  <body
  onUnload="if(!window.exitBlock)window.open('popup.html')">

    ...
    <a href="...">link</a>
    ...
    <script type="text/javascript" src="exitpage.js">
    </script>
  </body>
</html>

 
Note. This popup’s could be blocked by various popup-blockers.

You can redirect people to your other page or site on exiting your pages within the main window instead of showing popup windows.

Redirect in the same window

Lets imagine someone browsing your page. Now he doesn’t click your link but opens his bookmark instead. Suddenly he arrives your another page (an inquire or feedback page)…

Use a code like this:

<html>
  <body onUnload="if(!window.exitBlock)
    window.location.assign('http://mysite.com/feedback.html')"
>

    ...
    <script type="text/javascript" src="exitpage.js">
    </script>
  </body>
</html>

That’s all there is to it, follow this link for a more complete demonstration.

 

by Michel Komarov, © Copyright 2006. iCoder.com

 

Share in social bookmarking:These icons link to social bookmarking sites where readers can share and discover new web pages.  digg del.icio.us BlinkList Reddit NewsVine YahooMyWeb co.mments

Related Articles:

7 Responses to “Exit page redirect”

  1. vincent Says:

    great code, i will use this immediately on several of my sites. just one more way to add another contact with your visitor

  2. Trickrick Says:

    I need help with this code. I can get it to work on the pages but the Java Script is not working. In other words, it shows the exit page when I go to in-site links. Perhaps it is not compatible with my menu.

    Also, I wante3d to use it on this page:

    http://www.cardserviceinternational.com/wireless_apply_step2.htm

    My purpose is so they see my page if they exit at this point. However, when they click the submit button on the form, it takes them to the exit page instead of triggering the rest of the form process.

    So, for some reason the Java Script call is not working. It is viewing everything as an exit.

    I took the code off the page until I figure this out.

    Please help. I realy want to use this script.

  3. yourfriend20030 Says:

    This Js file and your code work well in the case of pressing Alt+F4 or Close Button in IE7. But It has a problem when I press F5 or Refresht button The session_End in global.aspx automatically raise when refresh a page.
    Can you help me to prevent it.
    Thanks in advance!

  4. michel Says:

    Unfortunately I’m not experienced with .NET
    and can’t help you with this issue.

    You need to check if your aspx script creates
    a new session every time it starts…

  5. iSeeU Says:

    Michel,

    Thank you, I’ve used your script on my website, it was easy to implement, and does exactly what I want it to do! I didn’t think it would be so easy!

    Would you be kind enough to answer my questions?

    1. Is it supposed to activate when the “refresh” button is pressed?

    2. Does it not work on Safari, Chrome & other browsers, but Firefox & Explorer?

    Thanks,

    Andrei

  6. michel Says:

    Hi Andrei,

    1. Yes, the “refresh” button triggers the onUnload event.
    So the script would work when you press F5.

    2. It was tested on Firefox and IExplorer only.
    However it even doesn’t work on new FF versions…

    Michel

  7. iSeeU Says:

    Yes, interesting, they must ‘av done something to it to make it ‘better’ for the users, arghhh.

    Спасибо за ответ, брат.

Got a question?   Leave a Reply

You must be logged in to post a comment.