简体   繁体   中英

onTab Change event in Javascript

Is there any onTab change event in javascript . Like whenwver the user is moved to new tab then an event is fired .

I have tried onfocus , onblur ,add listener but they are behaving differently in different browser. So is there any event that can be created when the tab is changed in java script ?

I tried the following links

Event for when user switches browser tabs

Also tried the https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

but again the events are not proper

The focus/blur events should work, but I suggest you listen on window and not document (if you did):

if (document.addEventListener) {
    window.addEventListener('focus', onFocus, 1);
    window.addEventListener('blur', onBlur, 1);
} else {
    // For IEs older than IE10
    document.attachEvent('onfocusin', onFocus);
    document.attachEvent('onfocusout', onBlur);
}

If this really doesn't work, the Page Visibility API should be covered enough and work properly. If you want to support some older browsers or just want to make sure to cover all possible cases, I recommend you use visibly.js by Addy Osmani .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM