【问题标题】:How to prevent popup using javascript?如何使用javascript防止弹出窗口?
【发布时间】:2011-04-18 05:06:47
【问题描述】:

无论如何我可以使用 javascript 来阻止来自另一个网站 (iframe) 的弹出窗口吗?

示例:我有一个网站,其中 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>

前提是这个弹出窗口不能被阻止并且用户在firefox或IE上的安全设置较低。

我有 ff。网站上的 iframe:(iframe.php)

<iframe src="http://friend.com/pop.php"></iframe>

在iframe.php页面上我应该怎么做才能防止弹出?

【问题讨论】:

    标签: javascript html popup


    【解决方案1】:

    这是不可能的。 wdm是对的。但这里提供了更详细的答案。

    How to block pop-up coming from iframe?

    【讨论】:

    • 我们可以 XSS 进入网站吗?还是只能检测?如果检测到,我可以使用脚本删除 iframe
    • 对不起,我从来没有这样做过,我什至找不到从 javascript 中检测弹出窗口的解决方案。您可以找到在 iframe 中打开的模态窗口,但无法在 javascript 中找到由 iframe 打开的普通窗口。检测的唯一方法是使用浏览器插件!
    【解决方案2】:

    我很确定你想要做的事情是不可能的。

    两个选项...

    1. 避免使用带有弹出窗口的 iframe 网站。
    2. 询问他们是否会删除弹出窗口或为您创建一个特殊的登录页面。如果您以某种方式与他们有关联,他们可能会帮助您。

    【讨论】:

      【解决方案3】:

      不,我不同意其他人的观点,html5 有一个沙盒属性,它控制通过 iframe 可以看到什么或可以执行哪些操作。

      只需在您的 iframe 中添加以下属性,它应该会阻止弹出窗口 "sandbox="allow-scripts allow-forms allow-same-origin""

      例如&lt;iframe src="source of your iframe" sandbox="allow-scripts allow-forms allow-same-origin"&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-07
        相关资源
        最近更新 更多