admin管理员组文章数量:1325236
Different web browsers handle the window.close() function differently. IE prompts the user for confirmation, while Firefox and Safari just fail to honor it unless the window was originally opened with Javascript and display a message saying as much in the console.
A third party web application used internally in our organization that I support displays a 'close' button at the end of a wizard-like series of pages. This works well for IE, which is what the majority of our users use. However, this obviously fails in FF. I'd prefer to leave the button in and use Javascript to gracefully degrade the UI by not displaying that button in any browser that will not perform the window.close().
As a rule of thumb, I try to check browser capability rather than relying on a hard-coded policy based on browser detection whenever possible. Is there a way to programmatically check the support for window.close() so I can determine whether the button should be displayed in the first place?
Different web browsers handle the window.close() function differently. IE prompts the user for confirmation, while Firefox and Safari just fail to honor it unless the window was originally opened with Javascript and display a message saying as much in the console.
A third party web application used internally in our organization that I support displays a 'close' button at the end of a wizard-like series of pages. This works well for IE, which is what the majority of our users use. However, this obviously fails in FF. I'd prefer to leave the button in and use Javascript to gracefully degrade the UI by not displaying that button in any browser that will not perform the window.close().
As a rule of thumb, I try to check browser capability rather than relying on a hard-coded policy based on browser detection whenever possible. Is there a way to programmatically check the support for window.close() so I can determine whether the button should be displayed in the first place?
Share Improve this question edited May 2, 2015 at 1:40 Sam Hanley 4,7557 gold badges37 silver badges64 bronze badges asked Oct 24, 2011 at 21:30 BrahmBrahm 1415 bronze badges 2- 2 I don't think it's possible. You're probably best off checking which browser is being used - if it's IE, you can assume that window.close() will work. If it's anything else, then assume it will not work. – Joseph Redfern Commented Oct 24, 2011 at 21:39
- Until IE follows other browsers and doesn't work as expected. Better to write to the developers of the web application and get them to fix it. – RobG Commented Oct 24, 2011 at 23:25
3 Answers
Reset to default 3Try this:
Demo: http://jsfiddle/ThinkingStiff/mnv87/
Script:
function hasClose() {
var close = window.open( '', '', 'height=100,width=100,left=3500', false );
close.close();
return close.closed;
};
if( hasClose() ) {
//show button
} else {
//hide button
};
Note hasClose()
will also return false
if popups are blocked.
Why not check patibility, and then append if patible? Using jQuery:
<script type="text/javascript" src="latest_jquery_file.js"></script>
<script type="text/javascript">
$(document).on("ready", (function(e)
{
$("body").append('<p><a href="#" id="windowcloser">Close The Window!!</a></p>');
$('#windowcloser').click(function(){
window.close();
});
})
);
</script>
Since jQuery is cross browser patible, it should
Very simple. Your script should try (or try
) to window.close
, and if its still alive after that try - show the message, and, optionally, erase/replace page content, or use location.reload
to not give your users any reason to stay at the page anymore.
p.s.: keep in mind, closing windows from JavaScript is very impolite. So you better have some good reasons for doing it ;)
本文标签: javascriptHow do I programmatically detect how a browser handles windowclose()Stack Overflow
版权声明:本文标题:javascript - How do I programmatically detect how a browser handles window.close()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166215a2425873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论