iFrame src 更改事件检测? [英] iFrame src change event detection?

查看:36
本文介绍了iFrame src 更改事件检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我无法控制 iframe 中的内容,有什么方法可以通过父页面检测其中的 src 更改?也许是某种加载?

Assuming I have no control over the content in the iframe, is there any way that I can detect a src change in it via the parent page? Some sort of onload maybe?

如果 iframe src 与之前相同,我的最后手段是进行 1 秒的间隔测试,但执行这个 hacky 解决方案会很糟糕.

My last resort is to do a 1 second interval test if the iframe src is the same as it was before, but doing this hacky solution would suck.

如果有帮助,我正在使用 jQuery 库.

I'm using the jQuery library if it helps.

推荐答案

您可能想要使用 onLoad 事件,如下例所示:

You may want to use the onLoad event, as in the following example:

<iframe src="http://www.google.com/" onLoad="alert('Test');"></iframe>

每当 iframe 中的位置发生变化时,就会弹出警报.它适用于所有现代浏览器,但可能不适用于一些非常旧的浏览器,如 IE5 和早期的 Opera.(来源)

The alert will pop-up whenever the location within the iframe changes. It works in all modern browsers, but may not work in some very older browsers like IE5 and early Opera. (Source)

如果 iframe 显示的页面位于父级的同一域中,您将能够使用 contentWindow.location 访问该位置,如下例所示:

If the iframe is showing a page within the same domain of the parent, you would be able to access the location with contentWindow.location, as in the following example:

<iframe src="/test.html" onLoad="alert(this.contentWindow.location);"></iframe>

这篇关于iFrame src 更改事件检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆