16

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

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

4

7 回答 7

10

如果您想阻止弹出式广告或来自您在 IFRAME 中显示的网站的内容 - 这相当容易。

制作iframe 指向的framefilter.phpjavascriptfilter.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>

帧过滤器.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;

}

?>
于 2014-01-28T08:25:59.570 回答
7

很老的问题,但我想我会提供一个更新的解决方案,因为这是谷歌的最佳结果。

如果要阻止 iframe 打开窗口,可以在 iframe 上使用新的 HTML5“沙盒”属性。

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

这应该可以防止它做任何事情(除了运行 javascript 可能需要页面正常运行):

<iframe sandbox="allow-scripts" src="your/url/here"></iframe>
于 2017-04-08T18:07:56.347 回答
2

实际上,这可能的。至少在很多情况下。通常,iframe 中的代码会运行类似于top.window.open(...)打开弹出窗口的操作。您可以重新定义 window.open 方法,使其仍然存在,但不会打开窗口。例如:

` window.alias_open = window.open;

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

如果您仍然希望打开一些弹出窗口,您可以将 url 中的 url 列入白名单window.open,并alias_open根据需要调用。

于 2012-10-09T04:13:45.473 回答
2

我不认为这是可能的。

  • 首先(也是最重要的),如果 iframe 位于不同的域中,则无法更改其 DOM - 例如 onunload 处理程序。如果是这样的话,其他两个问题就没有实际意义了。
  • 其次,即使可以,您也必须以某种方式删除侦听器。如果监听器是通过 window.onunload 加载的,那就很简单了;否则,没有那么多。
  • 第三,从长远来看,这将导致与框架破坏者一样的军备竞赛

我看到的唯一可能性本质上是非技术性的:与在 iframe 中运行该站点的人核实他们是否可以为您制作一个特殊页面,一个没有此类 onunload 弹出窗口的页面。在大多数情况下,要么

  • a) 可以进行一些特殊安排(尽管并不总是免费的),或
  • b) 删除该功能将违反 ToS,在这种情况下,您必须寻找提供类似功能的其他人,而没有弹出窗口(实际上,大多数服务都有不止一个提供商)
于 2011-02-10T10:28:53.407 回答
2

在 IFrame 元素上设置沙箱属性应该可以工作。

于 2015-05-28T23:46:13.047 回答
0

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

于 2012-08-27T01:33:55.777 回答
-8

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

于 2010-12-16T13:39:41.627 回答