javascript 如何使用javascript防止弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5698729/
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
How to prevent popup using javascript?
提问by DucDigital
Is there anyway I can use javascript to block popup from another website (iframe)?
无论如何我可以使用javascript来阻止来自另一个网站(iframe)的弹出窗口?
Example: I have a website, which iframe to several other sites. One of them has a popup script like this:
示例:我有一个网站,它 iframe 到其他几个网站。其中之一有这样的弹出脚本:
<script type="text/javascript">
var popunder=new Array()
popunder[0]="http://www.target.com"
//Specify the width and height of new popunder window (in pixels).
var width = '700';
var height = '450';
//these are obvious variables. set "yes" or "no".
var p = 'scrollbars=yes, resizable=yes, toolbar=yes,' + 'menubar=yes, status=yes, location=yes, left=85, top=20, height=' + height + ',width=' + width;
// Load new PopUnder only once per browser session? (0=no, 1=yes)
// Putting 0 will cause the Popunder to load every time page is loaded
// Specifying 1 will cause it to load only once per session
var one_time=0
// That's it! Don't edit the code below unless you're really good. :-P //
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if the cookie exists
offset += search.length
//set the index of beginning value
end = document.cookie.indexOf(";", offset);
if (end == -1) // set the index of the end of cookie value
end = document.cookie.length;
returnvalue = unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function loadornot(){
if (get_cookie('popunder')==''){
load_pop_power()
document.cookie="popunder=yes"
}
}
function load_pop_power(){
win2 = window.open(popunder[Math.floor(Math.random()*(popunder.length))], "bw", p)
win2.blur()
window.focus()
}
if (one_time==0)
load_pop_power()
else
loadornot()
</script>
Provide that this popup cannot be block and user have a low security setting on firefox or IE.
前提是无法阻止此弹出窗口,并且用户在 Firefox 或 IE 上的安全设置较低。
I have the ff. iframe on the site: (iframe.php)
我有 ff。网站上的 iframe: (iframe.php)
<iframe src="http://friend.com/pop.php"></iframe>
What should i do on the iframe.php page to prevent popup?
我应该在 iframe.php 页面上做什么来防止弹出?
采纳答案by Farzin Zaker
Its not possible. wdm is right. but a more detailed answer is provided here.
这是不可能的。wdm 是对的。但这里提供了更详细的答案。
回答by wdm
I'm pretty sure what you're trying to do is not possible.
我很确定你想要做的事情是不可能的。
Two options...
两种选择...
- Avoid iframing sites that have popups.
- Ask them if they'll remove the popup or create a special landing page for you. If you are affiliated with them somehow they may help you out.
- 避免使用具有弹出窗口的 iframe 网站。
- 询问他们是否会删除弹出窗口或为您创建一个特殊的登录页面。如果您以某种方式与他们有关联,他们可能会帮助您。
回答by Abdulbasit
No, I do not agree with others, html5 has a sandbox attribute which controls what is seen or what actions can be done through the iframe.
不,我不同意其他人的观点,html5 有一个沙箱属性,它控制通过 iframe 看到的内容或可以执行的操作。
just add the following attribute in your Iframe and it should block the pop-ups
"sandbox="allow-scripts allow-forms allow-same-origin""
只需在您的 iframe 中添加以下属性,它就会阻止弹出窗口“ sandbox="allow-scripts allow-forms allow-same-origin"”
eg <iframe src="source of your iframe" sandbox="allow-scripts allow-forms allow-same-origin">
例如 <iframe src="source of your iframe" sandbox="allow-scripts allow-forms allow-same-origin">

