Javascript to detect if user changes tab

后端 未结 5 1876
无人共我
无人共我 2020-11-30 22:46


I want to write a webpage for online quiz.Basic requirement i have is that, If the person taking quiz changes tab or opens new window even without minimizing browser, i.

5条回答
  •  难免孤独
    2020-11-30 23:42

    You can determine if a tab or window is active by attaching a blur / focus event listener to window.

    in jQuery it would be

    $(window).focus(function() {
        //do something
    });
    
    $(window).blur(function() {
        //do something
    });
    

    quoted from this SO answer: https://stackoverflow.com/a/1760268/680578

提交回复
热议问题