l2mt
l2mt

Reputation: 550

Doing things depending of the action in a web page with Jquery

Anyone know how to do this:

When some user left my site by closing the browser or tab or going to other site I would like to do an action (show an alert) and if the user clicks on a link or button to other page of my own site I would like to do another action...

If I use unload event I can't differentiate between these two kind of behavior of the user... please help because I really need it.

Thanks for the help.

Upvotes: 0

Views: 52

Answers (1)

nickf
nickf

Reputation: 546095

Set a flag on all of your internal links:

var isInternal = false;
$('a[href^="http://mysite.com/"]').live('click', function() {
    isInternal = true;
});

// then in your onunload handler:

if (isInternal) {
    // perform action 1
} else {
    // perform action 2
}

A better selector for the links might be something like this:

$('a:not([href*="//"])')

But only if you never use absolute links in your own site.

Upvotes: 1

Related Questions