使用jquery检测iframe src的变化 [英] Detect iframe src change using jquery

查看:698
本文介绍了使用jquery检测iframe src的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,一个< iframe> 最初被加载指向一个特定的src。用户点击一个超链接,iframe的内容发生变化。

Lets say an <iframe> is initially loaded pointing to a certain src. The user clicks on a hyperlink and the contents of the iframe changes.

我想检测iframe的新src。

I would like to detect that new src of the iframe in full.

不幸的是,当检测到变化时,似乎只提供了原始的src。它检测到这种变化,而不是它改变了什么。

Unfortunately it appears to only provide the original src when a change is detected. It detects the change, just not what it changed to.

也许这是一个特定于浏览器的问题。

Perhaps it is a browser-specific issue. I am using chrome.

$(function(){
    $('iframe').load(function() {
        var src = $("iframe").attr('src')
        alert(src);
    });
});

http://jsfiddle.net/vud8S/7/

推荐答案

幸运的是不可能从其他网域获取iframe的网址而不是其父网页。您将无法阅读完整的url。

Fortunately it is not possible to get an URL of an iframe from a different domain than it's parent page. You will not be able to read the full url.

如果您尝试阅读文档位置(其src是不同域的iframe)的文档位置,权限错误,看起来类似于这样的:错误:拒绝获取属性Location.href的权限

If you try to read the documents location (of an iframe whose src is a differnt domain) than you will get a permission error that looks similar to this: "Error: Permission denied to get property Location.href"

这是由于同源策略。更多细节: http://de.wikipedia.org/wiki/Same-Origin-政策

This is due to "Same Origin Policy". More details on that: http://de.wikipedia.org/wiki/Same-Origin-Policy

这篇关于使用jquery检测iframe src的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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