Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect when a visitor comes back to your page after opening a new window?

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,

like image 816
Jeremy Avatar asked Jul 13 '10 05:07

Jeremy


1 Answers

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

like image 142
jAndy Avatar answered Oct 25 '22 19:10

jAndy