简体   繁体   English

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

[英]How to block pop-up coming from iframe?

I'm embedding page that has an exit pop-up. 我正在嵌入具有退出弹出窗口的页面。 When you close the page, it automatically launches a pop-up window. 关闭页面时,它会自动启动弹出窗口。

How to disable pop-ups coming from the iframe on exit? 如何在退出时禁用来自iframe的弹出式窗口?

If you are wanting to block something like POP up ads or something coming from a website you are showing in an IFRAME - it's fairly easy. 如果你想阻止像POP广告这样的东西或来自你在IFRAME中展示的网站的东西 - 这很容易。

Make a framefilter.php and javascriptfilter.php which your iframe points to. 制作一个iframe指向的framefilter.phpjavascriptfilter.php You can modify it to meet your needs such as the onload blah blah and etc. But as/is - it's been working fine for me for quite a while. 您可以修改它以满足您的需求,例如onload blah blah等等。但是/是 - 它已经很好地为我工作了很长一段时间。 Hope it helps. 希望能帮助到你。

Replace your standard IFRAME HTML with this: 用以下内容替换标准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 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 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;

}

?>

Quite an old ask, but I thought I'd offer a newer solution since this is the top result in google. 相当古老的问题,但我认为我会提供更新的解决方案,因为这是谷歌的最佳结果。

If you want to block an iframe from opening windows, you can use the new HTML5 "sandbox" attribute on your iframe. 如果要阻止打开窗口中的iframe,可以在iframe上使用新的HTML5“沙盒”属性。

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

This should keep it from doing anything (except running javascript which may be required for the page to function correctly): 这应该阻止它做任何事情(除了运行javascript,这可能是页面正常运行所需):

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

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

Actually, this is possible. 实际上,这可能的。 Well at least in many cases. 好吧,至少在很多情况下。 Often, the code in the iframe will be running something like top.window.open(...) to open a pop-up. 通常,iframe中的代码将运行类似top.window.open(...)来打开弹出窗口。 You can redefine the window.open method so it still exists, but doesn't open a window. 您可以重新定义window.open方法,使其仍然存在,但不会打开窗口。 Eg: 例如:

` window.alias_open = window.open; `window.alias_open = window.open;

window.open = function(url, name, specs, replace) { // Do nothing, or do something smart... } ` window.open = function(url,name,specs,replace){//什么也不做,或做点聪明的事......}`

If you still want some pop-ups to open, you can whitelist urls within the body of window.open , and call alias_open as needed. 如果您仍然想要打开一些弹出窗口,可以在window.open的主体内将URL列入白名单,并根据需要调用alias_open

I don't think this is possible. 我不认为这是可能的。

  • first (and most importantly), if the iframe is in a different domain, you can't change its DOM - such as the onunload handlers. 首先(也是最重要的),如果iframe位于不同的域中,则无法更改其DOM - 例如onunload处理程序。 If this is the case, the other two issues are moot. 如果是这种情况,其他两个问题都没有实际意义。
  • second, even if you could, you'd have to remove the listener in some way. 第二,即使你可以,你也必须以某种方式移除听众。 If the listener is loaded via window.onunload, that would be simple; 如果通过window.onunload加载监听器,那很简单; otherwise, not so much. 否则,不是那么多。
  • third, in the long term this would lead to the same arms race as the frame-busting-busters 第三,从长远来看,这将导致与框架破坏者相同的军备竞赛

The only possibility I see is non-technical in nature: check with whoever runs that site inside the iframe if they could make a special page for you, one without such onunload popup. 我看到的唯一可能性是非技术性:如果他们可以为您创建一个特殊页面,请检查iframe中运行该站点的任何人,没有这样的onunload弹出窗口。 In most cases, either 在大多数情况下,要么

  • a) some special arrangement can be made (although not always for free), or a)可以做出一些特殊的安排(虽然不总是免费),或者
  • b) removing the functionality would be a violation of the ToS, in which case you'd have to look for someone else providing similar functionality, without the pop-ups (and realistically, most services have more than a single provider) b)删除功能将违反ToS,在这种情况下,您必须寻找提供类似功能的其他人,而不需要弹出窗口(实际上,大多数服务都有多个提供商)

I'm not sure if this would work but you could try double Iframing. 我不确定这是否有用,但你可以尝试双重Iframing。 Iframe the site in a free blogger account, then iframe the blogger account with a delay loading code. iframe网站在免费的博客帐户中,然后iframe博客帐户延迟加载代码。 so the popup will occur before the page is loaded let me know if it works. 因此弹出窗口将在页面加载之前发生,让我知道它是否有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM