我想使用JavaScript获取iframe的更新网址吗?

像这样:

<iframe src="http://yahoo.com" id="ifrm" onload="iframeurl()" ></iframe>
<script>
function iframeurl() {
var ifrm = document.getElementById("ifrm");
alert(ifrm.src);
}
</script>


但是上面的代码不起作用。我怎样才能做到这一点?
我将在iframe中使用外部链接,该链接不在我使用上述代码的同一域中托管。

请帮忙。

谢谢

再次感谢。 http://yahoo.com只是一个例子。同样,我的域名是szacpp.com,我需要获取other.com的链接。您的意思是我无权访问域时需要将htaccess放在other.com中。

最佳答案

当页面在iframe中更改时,iframe的src属性不会更新。

您唯一的选择是在iframe中查询文档,但是仅在iframe中的页面来自同一域时才有效(否则您按same origin policy

更新

如果您有权访问other.com,则应该能够在Access-Control-Allow-Origin文件中设置.htaccess(假设您正在使用使用该文件系统的服务器)。

您将找到更多信息...


http://www.w3.org/TR/cors/#access-control-allow-origin-response-header
http://enable-cors.org/
Access-Control-Allow-Origin Multiple Origin Domains?


如果您没有访问other.com的权限,那么您根本就不走运。您将不可能知道新页面是什么,因为在src中更改页面时iframeiframe属性将不会更新。由于前面提到的same origin policy(或更确切地说是Quentin),您将无法使用JavaScript。

关于javascript - iframe动态src,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11281089/

10-14 13:01
查看更多