admin管理员组文章数量:1026373
UPDATE
So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.
This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there?
Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.
<html>
<head runat="server">
<title></title>
<script type="text/javascript">
window.onunload = function() {
alert("unload event detected!");
}
</script>
</head>
<body>
</body>
</html>
UPDATE
So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.
This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there?
Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.
<html>
<head runat="server">
<title></title>
<script type="text/javascript">
window.onunload = function() {
alert("unload event detected!");
}
</script>
</head>
<body>
</body>
</html>
Share
Improve this question
edited Dec 28, 2013 at 4:00
ryanulit
asked Oct 6, 2010 at 20:59
ryanulitryanulit
5,0016 gold badges46 silver badges66 bronze badges
2 Answers
Reset to default 2You can't count on onunload
handlers firing at all (it's not just Firefox). onbeforeunload
handlers are slightly more reliable, but only slightly.
Update Responding to your follow-up question: If you have to use a lock, then yes, I would definitely use a server-side timeout (you have no other choice; if the user's workstation blue-screens, for instance, you want that record unlocked at some point). You probably want to reset the timeout any time you see activity from the user (you could use an ajax ping while the page is open) and, as you said, give the user a means of explicitly releasing the record. How long that timeout should be is up to you, but if you used the ajax ping mechanism, no reason it would even need to be five minutes — you could do a one-minute timeout and a 30-second ajax ping, or even a 30-second timeout and a 15-second ping, whatever. Depends on what kind of load you want to put on the server.
Firefox closes immediately after you click the X to close the browser. For some reason it 'forgets' to call the onunload event.
UPDATE
So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.
This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there?
Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.
<html>
<head runat="server">
<title></title>
<script type="text/javascript">
window.onunload = function() {
alert("unload event detected!");
}
</script>
</head>
<body>
</body>
</html>
UPDATE
So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.
This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there?
Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.
<html>
<head runat="server">
<title></title>
<script type="text/javascript">
window.onunload = function() {
alert("unload event detected!");
}
</script>
</head>
<body>
</body>
</html>
Share
Improve this question
edited Dec 28, 2013 at 4:00
ryanulit
asked Oct 6, 2010 at 20:59
ryanulitryanulit
5,0016 gold badges46 silver badges66 bronze badges
2 Answers
Reset to default 2You can't count on onunload
handlers firing at all (it's not just Firefox). onbeforeunload
handlers are slightly more reliable, but only slightly.
Update Responding to your follow-up question: If you have to use a lock, then yes, I would definitely use a server-side timeout (you have no other choice; if the user's workstation blue-screens, for instance, you want that record unlocked at some point). You probably want to reset the timeout any time you see activity from the user (you could use an ajax ping while the page is open) and, as you said, give the user a means of explicitly releasing the record. How long that timeout should be is up to you, but if you used the ajax ping mechanism, no reason it would even need to be five minutes — you could do a one-minute timeout and a 30-second ajax ping, or even a 30-second timeout and a 15-second ping, whatever. Depends on what kind of load you want to put on the server.
Firefox closes immediately after you click the X to close the browser. For some reason it 'forgets' to call the onunload event.
本文标签:
版权声明:本文标题:javascript - window.onunload only fires when a tab is closed in firefox, not the entire browser - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643992a2160915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论