SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Message on Exit of Website (not on exit of page)

    Hi There,

    I've only just registered here, however I have been using this site as a reference point for years.

    I am working on a website at the moment for a Games Recruitment Company and they have indicated that they would like to have a popup when someone trys to leave the website, saying something along the lines of:

    "Are you sure you wish to leave this website, click ok to proceed or cancel to stay on the website"

    I have found numerous scripts that work for leaving webpages, however this has the unfortunate effect of popping up when someone trys to visit pages within the same website.

    I require a script that will only generate a popup when someone attempts to leave the website as a whole - not just the specific page they are on.

    Any help would be very much appreciated.

    Thanks
    Zach

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    That sort of information is not available to any scripts. All you are able to determine is if someone is leaving the existing page. Not the whereabouts of their destination.

    Anyhow, if someone is leaving the website they are pretty sure that they want to leave, and you will most likely incur their wrath at being told that you don't want them to leave yet, which will reflect badly on the company name as well.

    It's a simple usability matter where you have to realise that as much as you may desire it to be otherwise, the user is in control.
    Jacob Neilsen's Usability Alertbox has lots more to say on the topic of usability too.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Basically as far as the web page is concerned the following are all identical

    - reloading the current page
    - leaving the page to go to another page on the same site
    - leaving the site to go to a different site
    - closing the browser

    There is no way for a web page to tell which of those four actions is being performed.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard bronze trophy bluedreamer's Avatar
    Join Date
    Jul 2005
    Location
    Middle England
    Posts
    3,361
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ask your recruitment company this...

    If you have a visitor to your office and they're ready to leave - do you ask them "are you sure you want to leave our office".

    Of course not

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you're asking for is really bad usability, BUT, for the sake of the question/academics, I'd look at JavaScript's location.href.

    You can use that to parse your url to get your domain, then, maybe on the onunload event, check to see if the domain has changed?

    I think a server-side technology like PHP would be better, though again, this is terrible usability, I'm just thinking out loud!

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It would only work if the JavaScript were attached to the following page where it would be sometimes possible to check what page the browser was displaying before.

    The only time you could do it from the page being unloaded is where the person selects a link within your page in order to jump to somewhere outside your site. You could then attach the processing you require to that link.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •