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

发布于 2024-10-08 02:44:17 字数 95 浏览 9 评论 0原文

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

如何禁用退出时来自 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怀念你的温柔 2024-10-15 02:44:17

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

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

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

这应该可以防止它执行任何操作(除了运行 javascript,这可能是页面正常运行所必需的):

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

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.

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):

<iframe sandbox="allow-scripts" src="your/url/here"></iframe>
2024-10-15 02:44:17

如果您想阻止 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>

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;

}

?>

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.

Make a framefilter.php and javascriptfilter.php which your iframe points to.
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. Hope it helps.

Replace your standard IFRAME HTML with this:

    <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;

}

?>
神爱温柔 2024-10-15 02:44:17

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

Setting the sandbox attribute on the IFrame element should work.

北音执念 2024-10-15 02:44:17

我认为这是不可能的。

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

我看到的唯一可能性本质上是非技术性的:与 iframe 内运行该网站的任何人联系,检查他们是否可以为您制作一个特殊的页面,一个没有这样的 onunload 弹出窗口的页面。在大多数情况下,要么

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

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. 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; 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. In most cases, either

  • a) some special arrangement can be made (although not always for free), or
  • 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)
柳絮泡泡 2024-10-15 02:44:17

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

`
window.alias_open = window.open;

window.open = 函数(url, 名称, 规格, 替换) {
// 什么也不做,或者做一些聪明的事情...
}
`

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

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. You can redefine the window.open method so it still exists, but doesn't open a window. E.g.:

`
window.alias_open = window.open;

window.open = function(url, name, specs, replace) {
// Do nothing, or do something smart...
}
`

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.

白昼 2024-10-15 02:44:17

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

I'm not sure if this would work but you could try double Iframing. Iframe the site in a free blogger account, then iframe the blogger account with a delay loading code. so the popup will occur before the page is loaded let me know if it works.

最近可好 2024-10-15 02:44:17

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

Use a modern browser - they all come with decent pop-up blocking capabilities

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文