如何以编程方式检测浏览器如何处理window.close()的? [英] How do I programmatically detect how a browser handles window.close()?

查看:215
本文介绍了如何以编程方式检测浏览器如何处理window.close()的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同的网络浏览器不同的方式处理的window.close()的函数。 IE浏览器会提示用户进行确认,而Firefox和Safari除非该窗口的最初使用Javascript刚开业没有履行它,并显示一条消息说尽可能多的在控制台中。

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.

在我们的组织,我支持一个显示关闭按钮,在精灵般的系列网页年底内部使用的第三方Web应用程序。这非常适用于IE浏览器,这是我们的大多数用户使用什么。然而,这显然无法在FF。我想preFER离开按钮,并使用JavaScript通过不显示在任何浏览器的按钮,将不执行window.close()的正常降级的用户界面。

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().

作为一个经验法则,我尝试检查浏览器的能力,而不是基于浏览器检测尽可能依靠硬codeD政策。有没有一种方法以编程方式检查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?

推荐答案

试试这个:

演示: http://jsfiddle.net/ThinkingStiff/mnv87/

脚本:

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
};

请注意 hasClose()也将返回如果弹出窗口被阻止。

Note hasClose() will also return false if popups are blocked.

这篇关于如何以编程方式检测浏览器如何处理window.close()的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆