0

I want my javascript to be trigged when:

  • The current IE tab is switched out when multiple IE tabs are open.
  • When the current IE tab is closed.
  • I don't want my JS code be trigged by the in-page pops up dialogs.
  • When the whole IE window closed.

The lose focus event may not work for me because there are pop up diaglogs in my page, so when it pops out, the IE tab will lose focus, but since the tab is not switched or closed, I don't want my javascript to be trigged here.

Is there any solution? I am wondering if there's something like entering tab / leaving tab, or tab-switching events?

Some interesting links, but not resolve my question.

Is there a way to detect if a browser window is not currently active?

Hook into tab changed event of browser

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482

3 Answers3

1

if you use 'jQuery', you can easily do it .

$(window).blur(function(){
  // your code 
});
$(window).focus(function(){
  // your code
});

here is the link which provides one more method to do it.

Community
  • 1
  • 1
  • jquery that is cheating the guy is using javascript. :D – vikingben May 21 '13 at 04:37
  • 1
    I am afraid this cannot handle the in-page pop-up dialog. The focus will fall on the dialog box, so the blur event will be raised for the window/tab. But I don't want it to be raised. – smwikipedia May 21 '13 at 04:53
  • true, this will not work for inpage popups :( , there are two more events 'onbeforeunload' 'onunload' try putting these on body tag.. i hope this will work.. – sameermkulkarni May 21 '13 at 05:06
1

you may be interested in

(function(){

    function doOnFocus(){ console.log("focus"); }
    function doOnBlur(){ console.log("blur"); }
    function doOnLeave(){ console.log("leave"); }

    if('onfocusout' in document){
        document.onfocusout = doOnBlur;
        document.onfocusin = doOnFocus;
    }else{
        window.onblur = doOnBlur;
        window.onfocus = doOnFocus;
    }
    window.onbeforeunload = doOnLeave;

})();
0

In javascript there is an event on window close it is not IE specific but is mostly used to call an alert before the user leaves the page. It's one of my pet peeves and is very annoying but may be what you are looking for.

window.onbeforeunload = yourfunctionthatexecutes;

vikingben
  • 1,632
  • 2
  • 24
  • 37