views:

676

answers:

7

This sounded like something almost impossible to do when it was presented to me. I know you can display a dialog box to confirm when leaving a web page. But is it possible to display a dialog box when leaving a site?

I haven't been able to find/create anything that can read the address bar and know that you're leaving the site.

+1  A: 

Your best bet is listening on the non-standard beforeunload event. This is supported by almost all browsers, expect of Opera which is known to adhere the W3C standards extremely strictly.

Kickoff example:

window.onbeforeunload = function() {
    return "You're leaving the site.";
};

This message will show up in kind of a confirmation dialogue.

In your specific case you need to turn it off (just set to null) whenever a navigational link is clicked or an internal form is submitted. You can do that by listening on the click event of the desired links and the submit event of the desired forms. jQuery may be of great help here:

window.onbeforeunload = function() {
    return "You're leaving the site.";
};
$(document).ready(function() {
    $('a[rel!=ext]').click(function() { window.onbeforeunload = null; });
    $('form').submit(function() { window.onbeforeunload = null; });
});

You only need to give all external links the defacto standard attribute rel="ext" to denote that those are external links.

<a href="http://google.com" rel="ext">Google</a>
BalusC
+1  A: 

Take a look at this thread.

One possible way to achieve this would be to use Javascript to examine all of the a tags on your page when it loads and check if they are linking to an external site. If so, you can add an onclick event to show a confirm/alert box or something more elegant. Of course, using jQuery will greatly simplify the Javascript you'll have to write, like in the above thread.

wsanville
+1  A: 

Using the expression from this question, you can do the following:

$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname);
};
$.expr[':'].internal = function(obj){
    return obj.hostname == location.hostname;
};

$(function() {
    var unloadMessage = function() {
        return "Don't leave me!";
    };

    $('a:internal').click(function() { 
        window.onbeforeunload = null;
    });
    $('form').submit(function() { 
        window.onbeforeunload = null;
    });

    $('a:external').click(function() { 
        window.onbeforeunload = unloadMessage;
    });

    window.onbeforeunload = unloadMessage;
});
Justin Johnson
Doesn't work when the user changes the URL in address bar.
BalusC
Updated -------
Justin Johnson
+6  A: 

First off define which events can actually take your user away from your site?

  1. A click of a link inside your web site content
  2. A submit of a form to an outside action
  3. A javascript from a child window that changes window.location on its parent
  4. User starting a search in the search bar (FF and IE)
  5. User entering a search/address in the browser address bar.
  6. User hitting a back button (or backspace) when it just came to your site
  7. User hitting a forward button (or shift-backspace) when they were off the site before but came back by getting there via Back button functionality
  8. User closes the browser window

So. what can you do about all these?

  1. These are easy. Check your anchors and if they do point outside, add some functionality in the onclick event
  2. Similar to 1. Add your functionality for the onsubmit event of the form posting back outside of your site.
  3. -> 8. don't really have an applicable solution that could be controlled. You can abuse onbeforeunload event as much as you want, but you won't have much success of knowing what's going on. And there are certain limitations related to onbeforeunload as well, so your hands will be tied most of the time.

The real question?

Why would you want to control this event anyway except for bothering your users not to leave you. Begging doesn't give much justice in the web world anyway. And when some site would bother me with messages or even worse prevent me from leaving I wouldn't want to get back anymore. It smells of bad bad bad usabily and gives a hint of adware site.

Rather try to keep your users interested by providing them with valuable content.

Robert Koritnik
This event is indeed often abused in terms of UX. But there are **certainly** real world cases wherein this event is considered mandatory. E.g. in lengthy forms and/or extremely confidental websites (i.e. banking).
BalusC
Well it's an intranet site for government. The network folks usually control access through the firewall. I don't know why they want this, too.
tahdhaze09
+1  A: 

It's possible. Just try entering a question or answer to SO and then navigating away before submitting it. It doesn't matter whether you click on a link or type in the address bar, you get an "Are you sure?" alert. You might post over on SO Meta asking how they do this.

dnagirl
+1  A: 

You can do that if you design your site as a one page web app.
It means, a single page is loaded, then other contents are loaded dynamically using ajax.

In that case the onbeforeunload is triggered when the user leave the page/site.

Mic
A: 

I'm needing something very similar, however, instead of relying on any rel, id, or onClick designations in the html's anchor tag, I need a way for the javascript to popup a warning if it's determined that the link a user clicks on is outside of our domain or a https site. Content Managers are able to add their own links, and it's not an option to have them insert the designations with every outside anchor tag link.

I'm not very good with javascript, so your help is greatly appreciated. Thank you.

Tim