最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detects a click outside document - Stack Overflow

programmeradmin3浏览0评论

I am having a problem to detect a click event outside document, for instance in the closing button of the browser without using the onbeforeunload event, because I have a JSP page that is processing something, using meta refresh to give the status of the process evolution.

Since I am using meta refresh I cannot use window.onbeforeunload event to prevent/confirm the user to exit because the meta refresh will fire up the event. Thus, I need to check manually if the mouse will be clicked outside my document.

I can check if the mouse coordinates are outside, thus can´t associate an click event to that in IE8.

if (window.event.clientY < 82 && window.onclick)

Someone have any idea out achieve this issue?

Thanks in advance!

I am having a problem to detect a click event outside document, for instance in the closing button of the browser without using the onbeforeunload event, because I have a JSP page that is processing something, using meta refresh to give the status of the process evolution.

Since I am using meta refresh I cannot use window.onbeforeunload event to prevent/confirm the user to exit because the meta refresh will fire up the event. Thus, I need to check manually if the mouse will be clicked outside my document.

I can check if the mouse coordinates are outside, thus can´t associate an click event to that in IE8.

if (window.event.clientY < 82 && window.onclick)

Someone have any idea out achieve this issue?

Thanks in advance!

Share Improve this question edited Oct 19, 2011 at 9:42 nfechner 17.6k7 gold badges47 silver badges64 bronze badges asked Oct 19, 2011 at 9:41 user1002813user1002813 112 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Detecting the close button isn't possible but you can detect if the user is losing focus of the browser by doing:

$(window).blur(function() {
    alert('lost focus');
}

It's not possible. Events don't fire outside of the document, including clicks on the window chrome.

I think you will need to think about what you're trying to achieve. It sounds like a shaky design if you must get the close event of the page. Lots of other events will affect you if that is of a concern.

If you have a JSP page producing and showing the status by a meta refresh - what is your problem with the window closing? That should be of your concern, not how to detect a browser close event.

发布评论

评论列表(0)

  1. 暂无评论