开发者

Pop alert when window/tab is not active

I am trying for quite some time here but with no luck.

Do you have any idea how you can make a pop-up (alert) show, whenever the user changes tab/window from his browser?

So basically, when the user changes a window at his/her browser, the alert window will开发者_如何学Go pop and the user will have to press the ok button to continue

This funcitonality is needed as I am creating an online tests site and I need to display the alert whenever the users will try to change tab/window


You can use the focus and blur events on the window object (tabs are considered windows, too.) Try this out:

window.addEventListener('focus', function() { console.log('Window has focus'); });
window.addEventListener('blur', function() { console.log('Window lost focus'); });

or with jQuery:

$(window).focus(function() { console.log('Window has focus'); });
$(window).blur(function() { console.log('Window lost focus'); });

Also, be careful about using alerts in this manner. Many users may find it a bit annoying.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜