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

Pau*_*aul 14 html javascript iframe popup onunload

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

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

小智 8

如果你想阻止像POP广告这样的东西或来自你在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>
Run Code Online (Sandbox Code Playgroud)

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;

?>
Run Code Online (Sandbox Code Playgroud)

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;

}

?>
Run Code Online (Sandbox Code Playgroud)


Sol*_*ace 7

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

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

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

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

<iframe sandbox="allow-scripts" src="your/url/here"></iframe>
Run Code Online (Sandbox Code Playgroud)


m.e*_*son -10

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

  • 在这种情况下,不要嵌入具有不良行为的代码 (2认同)