Jquery/javascript检测iframe是否可以加载

发布于 2024-10-08 15:57:49 字数 214 浏览 0 评论 0原文

我有来自另一个服务器/域的带有 OpenX 广告的 iframe。

当此广告服务器发生问题时,主服务器不会加载全部内容,因为 openx 在 iframe 中加载的域没有响应。我一直认为 iframe 独立于主站点工作,但如果域根本不响应,它就不会工作...

无论如何,主站点可以以某种方式检测到 iframe 中的 url 没有响应并跳过加载 iframe 并显示其余部分网站的?

I have iframes with OpenX ads I server from another server/domain.

When something happens to this ad server the main server doesn't load whole contents because the domain that openx loads in iframe is not responding. I always thought that iframe works independently from the main site but it doesn't if the domain doesn't answer at all...

Anyway can main site detect somehow that a url in iframe is not responding and skips loading iframe and show the rest of the site?

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

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

发布评论

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

评论(5

无尽的现实 2024-10-15 15:57:49

网站加载后加载 iFrame 怎么样?使用 jQuery(甚至使用 window.load 事件的纯 JavaScript)可以很容易地做到这一点。

因此,您不必“检测” iFrame 是否已加载,而是可以在网站的其余部分完成加载后加载它。 (抱歉过度使用“load”一词)

在 jQuery 中,您可以简单地将 url 附加到 document.ready 事件上的 iFrame。

空白 iFrame

<iframe id="iframe-ad" width="200"></iframe>

简单的 jQuery,用于在 document.ready 上加载 URL

<script type="text/javascript">
    $(document).ready(function() {
        $("#iframe-ad").attr("src", "http://www.google.com");
    });
</script>

How about loading the iFrame once the website is loaded? It's pretty easy to do this using jQuery (or even plain javascript using the window.load event).

So rather than wanting to 'detect' whether the iFrame has loaded, you can load it AFTER the rest of the website has completed loading. (sorry for excessive use of word 'load')

In jQuery, you can simply attach the url to the iFrame on the document.ready event.

A blank iFrame

<iframe id="iframe-ad" width="200"></iframe>

Simple jQuery to load the URL on document.ready

<script type="text/javascript">
    $(document).ready(function() {
        $("#iframe-ad").attr("src", "http://www.google.com");
    });
</script>
趴在窗边数星星i 2024-10-15 15:57:49

不幸的是,没有简单的方法可以做到这一点,除非它们是从同一个域提供的。我知道有一种方法可以让 iframe 内的 javascript 对它所包含的父文档执行一些操作,但我不太确定如何...

Unfortunately there is no easy way to do this unless they are served from the same domain. I know there is a way to get let the javascript inside the iframe perform some actions on the parent document it is contained in, but I am not really sure how...

一抹微笑 2024-10-15 15:57:49

这是不可能的,因为同源策略,在某些浏览器中存在一些“差距”。但这不会推荐!

It is not possible, because of Same origin policy, there are some "gaps" in some browsers. But this is not going to recommend!

后来的我们 2024-10-15 15:57:49

可能无法获得最佳体验,但您可以制作本地重定向文件。就像这样:

<iframe src="http://www.mydomain.com/redir?url=http://www.theirdomain.com/ads"/>

然后 redir 页面就返回

<script>
    location.href = "${url}";
</script>

这样,只要您的服务器正在响应,其他一切都会在 iframe 重定向时照常继续?

Might not make for the best experience, but you can make a local redirect file. something like:

<iframe src="http://www.mydomain.com/redir?url=http://www.theirdomain.com/ads"/>

then the redir page just returns

<script>
    location.href = "${url}";
</script>

That way as long as your server is responding, everything else will continue as normal while the iframe redirects?

别把无礼当个性 2024-10-15 15:57:49

如果我没有 iframe 只是来自不同域的 javascript 怎么样?如果域没有响应,JavaScript 将阻止页面加载。有办法防止这种情况吗?

How about if I don't have iframe just javascript originating from different domain? If the domain is not responding javascript holds the page back not to load. Is there a way to prevent that?

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