如何阻止来自iframe的弹出窗口?

时间:2010-12-16 13:36:54

标签: javascript html iframe popup onunload

我正在嵌入具有退出弹出窗口的页面。当您关闭页面时,它会自动启动一个弹出窗口。

如何在退出时禁用来自iframe的弹出窗口?

7 个答案:

答案 0 :(得分:8)

如果您想阻止像POP广告或来自您在IFRAME中展示的网站的内容 - 这很容易。

制作iframe指向的 framefilter.php javascriptfilter.php 。 您可以修改它以满足您的需求,例如onload blah blah等。 但是/是 - 它已经为我工作了很长一段时间。希望它有所帮助。

将此标准IFRAME HTML替换为:

    <IFRAME SRC="http://www.yourdomainhere.com/framefilter.php?furl=http://www.domainname.com" WIDTH=1000 HEIGHT=500>
If you can see this, your browser doesn't 
understand IFRAMES. However, we'll still 
<A HREF="http://www.domainname.com">link</A> 
you to the page.
</IFRAME>

<强> Framefilter.php

        <?php

//Get the raw html.
$furl=trim($_GET["furl"]);
$raw = file_get_contents($furl);

$mydomain="http://www.yourdomainhere.com/";

//Kill anoying popups.
$raw=str_replace("alert(","isNull(",$raw);
$raw=str_replace("window.open","isNull",$raw);
$raw=str_replace("prompt(","isNull(",$raw);
$raw=str_replace("Confirm: (","isNull(",$raw);

//Modify the javascript links so they go though a filter.
$raw=str_replace("script type=\"text/javascript\" src=\"","script type=\"text/javascript\" src=\"".$mydomain."javascriptfilter.php?jurl=",$raw);
$raw=str_replace("script src=","script src=".$mydomain."javascriptfilter.php?jurl=",$raw);

//Or kill js files
//$raw=str_replace(".js",".off",$raw);

//Put in a base domain tag so images, flash and css are certain to work.
$replacethis="<head>";
$replacestring="<head><base href='".$furl."/'>";
$raw=str_replace($replacethis,$replacestring,$raw);

//Echo the website html to the iframe.
echo $raw;

?>

<强> javascriptfilter.php

<?php

//Get the raw html.
$jurl=trim($_GET["jurl"]);
$raw = file_get_contents($jurl);

//Note, if trickyness like decode detected then display empty.
if(!preg_match("decode(", $raw)){

//Kill anoying popups.
$raw=str_replace("alert(","isNull(",$raw);
$raw=str_replace("window.open","isNull",$raw);
$raw=str_replace("prompt(","isNull(",$raw);
$raw=str_replace("Confirm: (","isNull(",$raw);

//Echo the website html to the iframe.
echo $raw;

}

?>

答案 1 :(得分:3)

相当古老的问题,但我认为我提供了一个更新的解决方案,因为这是谷歌的最佳结果。

如果要阻止打开窗口中的iframe,可以使用新的HTML5&#34;沙盒&#34;您的iframe属性。

https://developer.mozilla.org/en/docs/Web/HTML/Element/iframe

这应该阻止它做任何事情(除了运行javascript,这可能是页面正常运行所必需的):

<iframe sandbox="allow-scripts" src="your/url/here"></iframe>

答案 2 :(得分:2)

在IFrame元素上设置沙箱属性应该有效。

答案 3 :(得分:1)

我认为这不可能。

  • 首先(最重要的是),如果iframe位于不同的域中,则无法更改其DOM - 例如onunload处理程序。如果是这种情况,其他两个问题都没有实际意义。
  • 第二,即使你可以,你也必须以某种方式移除听众。如果通过window.onunload加载监听器,那很简单;否则,没那么多。
  • 第三,从长远来看,这将导致与frame-busting-busters
  • 相同的军备竞赛

我看到的唯一可能性是非技术性:如果他们可以为您创建一个特殊页面,请检查iframe中运行该站点的人,没有这样的onunload弹出窗口。在大多数情况下,要么

  • a)可以做出一些特殊安排(虽然并非总是免费),或
  • b)删除功能将违反ToS,在这种情况下,您必须寻找其他提供类似功能的人,而不需要弹出窗口(实际上,大多数服务都有多个提供商)

答案 4 :(得分:1)

实际上, 是可能的。好吧,至少在很多情况下。通常情况下,iframe中的代码会运行类似top.window.open(...)的内容来打开弹出窗口。您可以重新定义window.open方法,使其仍然存在,但不会打开窗口。 E.g:

` window.alias_open = window.open;

window.open = function(url,name,specs,replace){   //什么都不做,或做些聪明的事...... } `

如果您仍想要打开某些弹出窗口,可以在window.open正文中将网址列入白名单,并根据需要致电alias_open

答案 5 :(得分:0)

我不确定这是否有效,但您可以尝试双重Iframing。 iframe网站在免费的博客帐户中,然后iframe博客帐户延迟加载代码。所以弹出窗口会在页面加载之前发生,让我知道它是否有效。

答案 6 :(得分:-5)

使用现代浏览器 - 它们都具有不错的弹出窗口阻止功能

相关问题