How to use this piece of code in a function
Posted
by
oliverbj
on Stack Overflow
See other posts from Stack Overflow
or by oliverbj
Published on 2013-10-22T15:47:59Z
Indexed on
2013/10/22
15:53 UTC
Read the original article
Hit count: 198
JavaScript
|jQuery
I have this jQuery code:
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
vis(function(){
document.title = vis() ? 'Visible' : 'Not visible';
});
What it does now is to change the document title of the page. If the page is not visible, it will be changed to that and vise verca.
My question is, how can I use this function like this:
if page is visible{
//do something
}
if page is not visible{
//do something else
}
© Stack Overflow or respective owner