1

I am using the code below to check when the user changes the current tab.
It works great but how can i run a jquery code insted changing the document title?
I am trying to display a box when the user leaves the current tab/page and when the user come back to see it.

<script>
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';


//I tried

   $('.alertbox').show();

});
</script>

<div class="alertbox" style="display:none;">Welcome Back</a>

check this out: http://daniemon.com/tech/webapps/page-visibility/

Scimonster
  • 32,893
  • 9
  • 77
  • 89
Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • possible duplicate of [Best way to detect when a user leaves a web page?](http://stackoverflow.com/questions/147636/best-way-to-detect-when-a-user-leaves-a-web-page) – Andy Nov 18 '14 at 13:06
  • And save/load a cookie to register when the user comes back to that page. – Andy Nov 18 '14 at 13:07
  • 1
    onbeforeunload only works when the page is closed completely (unloaded) am I wrong? And popular browsers do not run JS while the tab is not active. I have not a clue to do such a thing. – Ali Naci Erdem Nov 18 '14 at 13:09
  • 1
    could also try catching window blur event – charlietfl Nov 18 '14 at 13:16
  • hey guys... check the link that i posted – Irene T. Nov 18 '14 at 13:35
  • And ofcourse it's not a duplicate question... I don't want to ask user if wants to leave the page.. instead to run a function or execute a jquery – Irene T. Nov 18 '14 at 13:38

0 Answers0