我想使用 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 中,而我无法访问该域。
最佳答案
src
当 iframe 内的页面发生更改时,iframe 的属性将不会更新。
您唯一的选择是查询 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
iframe
的属性iframe
内页面变化时不会更新。由于same origin policy,您将无法使用JavaScript我(或更准确地说,昆汀)之前提到过。
关于javascript - iframe动态src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281089/