views:

44

answers:

2

Is there a way, using JavaScript or jQuery, to detect when someone goes back to your page after opening a new window or tab?

I want to create a script that opens a new window or tab, and then does something when the user comes back to the page.

Thanks,

A: 

You could use cookies, but that's not flawless. If you can use a server-side language, then you can track IP addresses.

icktoofay
Oh, I was under the impression that they were closing the window and then re-opening it... ignore my answer, jAndy's is better in your case.
icktoofay
Thanks for your help
Jeremy
+5  A: 

yes, there is. Using jQuery:

$(window).bind('focusout', function(){
    console.log('bye bye');
});

$(window).bind('focusin', function(){
    console.log('welcome back!');
});

edit 1

Using alert() was not the best idea there :p Changed to console.log() for demonstration.

edit 2

binding to document does not work crossbrowser, changed to window

jAndy
Beautiful!Thanks :)
Jeremy