1

I need determine, if current page is active. I know i can find if tab get/lost focus with this answer:

https://stackoverflow.com/a/1760268/449553

It is ok, if tab state change, BUT i need to get initial value. There is a few ways to open page:

  • going by link in this active tab
  • open in new tab
  • open in new background tab

I understand, that page need some time to load. So I need to get this value after DOM loaded. Is there any way to find this value?

Community
  • 1
  • 1
msangel
  • 8,829
  • 3
  • 43
  • 64
  • use $(document).ready(function(){// your code }); that runs your code once DOM is fully loaded – Mody Oct 21 '13 at 07:02
  • @Ahmad ok, I use, but what code i shuld call? There are ONLY EVENTS WHEN STATE CHANGE. There is none values for reading. – msangel Oct 21 '13 at 07:04
  • you can go with settimeinterval and check that css of your tab is in active mode or not – Gyan Chandra Srivastava Oct 21 '13 at 07:05
  • Well there are few ways, You can add a hashtag to href of all links and then check the URL for the hash to find out wich tab is active – Mody Oct 21 '13 at 07:06
  • @GyanChandraSrivastava, what css selector is responsible for this operation? can you give me some example? – msangel Oct 21 '13 at 07:10
  • @Ahmad I don't see how that would help OP. OP wants to know the initial 'focus' value of a browser tab. If the user left-clicked on his link, the link opened in the same tab, so it would be focussed by default. On the other hand, if the user middle-clicked, it would open in a new tab and is not focussed by default. – Sumurai8 Oct 21 '13 at 07:11
  • @msangel as selected tab should have different CSS than Idle once so at the time of time interval you can go with check that desired one is having active css or not if that having active css you will get your solution – Gyan Chandra Srivastava Oct 21 '13 at 09:46
  • @GyanChandraSrivastava, a have tested styles via `getComputedStyle`- thay are same. – msangel Oct 21 '13 at 09:55
  • can you please send an example link of yours that we will give specific solution for you – Gyan Chandra Srivastava Oct 21 '13 at 09:59
  • Problem 'temporary' solved this way - in default tab is not active, but mouse move and keypress events on root document object change it to active. look like a trick, but in general - this meet my requirement. In future i hope new Page Visibility API will work as declared in document: [Page Visibility API on W3C](http://www.w3.org/TR/2011/WD-page-visibility-20110602/). Currently even my latest chrome doesn't support it as expected. – msangel Oct 22 '13 at 04:09
  • What exactly do you mean by "tab"? Browser tab? jQueryUI tab? Tab the soft drink? – Dom Oct 29 '13 at 22:13

1 Answers1

0

Try pagevisibility:

var visibilityChange,hidden, state; 
if (typeof document.hidden !== "undefined") {
    hidden = "hidden";
    visibilityChange = "visibilitychange";
    state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
    visibilityChange = "mozvisibilitychange";
    state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
    state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
    state = "webkitVisibilityState";
}

document.addEventListener(visibilityChange, function() {
    document.title = document[state];
}, false);


document.title = document[state];
if(document[state]==="hidden"){
    //hidden
}else{
    //show
}

See the compatibility

Nickolas
  • 101
  • 6
  • I saw this code. And I have already wtitten - ` In future i hope new Page Visibility API will work as declared in document: Page Visibility API on W3C. Currently even my latest chrome doesn't support it as expected.`. So yes. I general case - this work. But, not as expected - the initial value is invalid. – msangel Oct 29 '13 at 23:58
  • Is there a short & shiny way to do this with jQuery? – SIGSTACKFAULT Oct 10 '17 at 14:04