javascript 合法地避免弹出窗口阻止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6192889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Legally avoiding popup blocking
提问by giles
What is causing some browsers to see my code as unsolicited?
是什么导致某些浏览器将我的代码视为未经请求的?
I have a web site devoted to helping people with interactive sessions. It starts with the user clicking [Begin] so this is a consented action. This should (1) open a popup while (2) redirecting the first page to a end page as below :
我有一个专门帮助人们进行交互式会话的网站。它从用户单击 [开始] 开始,因此这是一个同意的操作。这应该 (1) 打开一个弹出窗口,同时 (2) 将第一页重定向到结束页,如下所示:
<head>
<SCRIPT language="JavaScript">
function openwindow(){window.open("{INTERACTION}","interaction","resizable=0,width=800,height=600,status=0");}</SCRIPT>
</head>
<body>
<FORM action="end.php" method="{METHOD}" >
<input type="submit" class="button"
onClick="javascript: openwindow()"
value="Begin" />
</FORM>
</body>
As said, this is not trying to open an unrequested popup but some strains of IE and Chrome appear to be treating it as such. I have been trying to get a fix, most recently digesting this post.
如前所述,这并不是试图打开一个未经请求的弹出窗口,而是 IE 和 Chrome 的某些菌株似乎是这样对待它的。我一直在尝试修复,最近在消化这篇文章。
In it Bobince comments
在里面 Bobince 评论
these days, you don't really need to ask the question “was my unsolicited popup blocked?”, because the answer is invariably “yes”?—?all the major browsers have the popup blocker turned on by default. Best approach is only ever to window.open() in response to a direct click, which is almost always allowed.I'm quite happy to buy into this principle because I simply want my popup to open.
这些天,你真的不需要问“我的主动弹出窗口被阻止了吗?”这个问题,因为答案总是“是”? - 所有主要浏览器都默认打开了弹出窗口阻止程序。最好的方法是只对 window.open() 响应直接点击,这几乎总是允许的。我很高兴接受这个原则,因为我只是想让我的弹出窗口打开。
What is causing some browsers to see my code as unsolicited?
是什么导致某些浏览器将我的代码视为未经请求的?
I'd appreciate any help you could give me. (as you might have guessed, client side is not my bag and this topic has been bugging me for ages).
我很感激你能给我的任何帮助。(您可能已经猜到了,客户端不是我的包,这个话题已经困扰我多年了)。
Many thanks in advance (and fingers crossed) Giles
非常感谢(和手指交叉)贾尔斯
采纳答案by Andreas
No much you can do. You could ask your users to disable pop-up blockers or inform them that a pop-up blocker is enabled by checking the window object ref returned by window.open()
你无能为力。您可以要求您的用户禁用弹出窗口阻止程序或通过检查 window.open() 返回的窗口对象引用来通知他们已启用弹出窗口阻止程序
e.g.
例如
var w = window.open('http://domain.com');
if(!w) {
//an alert in this example
alert('oops..seems like a pop-up blocker is enabled. Please disable');
}
you could find another way and try what Brad suggests.
你可以找到另一种方法并尝试布拉德的建议。
回答by Brad
There isn't anything you can do about this. Some popup blockers still block everything, even in response to a user clicking. The best you can do is suggest your users turn off popup blockers, or find a different way to do what you want to do. A popular method is the div that appears on top of all others on your page, like Lightbox.
对此你无能为力。一些弹出窗口阻止程序仍然会阻止所有内容,即使是响应用户点击。您能做的最好的事情就是建议您的用户关闭弹出窗口阻止程序,或者找到一种不同的方式来做您想做的事情。一种流行的方法是将 div 显示在页面上所有其他页面的顶部,例如Lightbox。
There are many jQuery pluginswhich make this easy.
有许多 jQuery 插件可以使这变得容易。
回答by haylem
You have (at least?) 2 options to deal with this:
你有(至少?)2个选项来处理这个问题:
- if you want to keep using popups, display a very visible warning for your users, pointing them to instructions on how to configure their browser to whitelist your domain (like the banners that appear on top of StackOverlow.com when you gain new privileges, or even like the banners Chrome is showing for actions - they are web-based as well);
- use an iFrame and load its content based on your user's click.
- 如果您想继续使用弹出窗口,请向您的用户显示一个非常明显的警告,向他们指出如何配置他们的浏览器以将您的域列入白名单的说明(例如当您获得新权限时出现在 StackOverlow.com 顶部的横幅,或甚至就像 Chrome 为操作而显示的横幅一样 - 它们也是基于网络的);
- 使用 iFrame 并根据用户的点击加载其内容。