iframe网址为专家提问

时间:2009-09-13 17:22:01

标签: javascript iframe

我希望有人可以帮助我,我想在iframe中获取网页的网址:

<script language="JavaScript">
    function myLocation() {
        alert(document.all.myFrame.contentWindow.location);
    }
</script>

<button onclick="myLocation();">Location of Frame</button>

<iframe src="http://www.google.com" width="800" height="600" frameborder="0"
                              scrolling="auto" name="myFrame"></iframe>

这基本上会在我的页面中给我iframe的位置,但是没有给我google.com网址 - 给我一些我可以从谷歌搜索结果打开的每个网页的网址也很重要,所以当我在google.com上它应该给我google.com网址,如果我浏览www.facebook.com它应该给我facebook的网址...

欢迎任何想法!感谢

2 个答案:

答案 0 :(得分:3)

same origin policy阻止访问其他域上的文档的详细信息(包括当前URI)。

如果您想要解决此问题,您将需要使用网页以外的内容(例如独立应用程序或浏览器扩展程序)或通过您自己的服务器代理所有请求。

答案 1 :(得分:0)

您可能想使用某种javascript库,但我在deviantart.com上使用原始javascript代码尝试了此代码:

document.getElementsByTagName('iframe')[0].src它给了我期待的结果。