Javascript 我想从 iframe 获取 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12069102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:03:12  来源:igfitidea点击:

i want to get url from iframe

javascript

提问by qlayer

this is the code im using.............

这是我使用的代码........

i want to get currenturl(href) from below code ...............

我想从下面的代码中获取当前的url(href) .....................

<html>
<head>
<script type="text/javascript">
        function load() {
            var url =  document.getElementById("iframeid");  

          //get the ifram onload url of href 
    //(i.e)   http://www.image.com/home.html     

        }
    </script>
</head>
<body>
<iframe onload="load()" name="Google"  id="iframeid"  scrolling="auto" style="width:95%;height:600px" src="http://www.image.com" >

    </iframe>
</body>
</html>

回答by qlayer

This will work only if they are in the same domain,protocol and port:

这仅在它们位于相同的域、协议和端口中时才有效:

document.getElementById("iframe_id").contentWindow.location.href

else use :

否则使用:

document.getElementById("iframe_id").src

回答by xdazz

The demo.

演示。

<html>
<head>
<script type="text/javascript">
        function load(frame) {
            console.log(frame.src);
        }
</script>
</head>
<body>
<iframe onload="load(this)" name="Google"  id="iframeid"  scrolling="auto" style="width:95%;height:600px" src="http://www.image.com" >
</iframe>
</body>
</html>

回答by Coder

try this. Sometimes you can't acess the Iframe url

尝试这个。有时您无法访问 Iframe 网址

var CurrentUrl = document.getElementById('MyIFrame').contentWindow. location.href;

回答by sQVe

Try this:

尝试这个:

document.getElementById("iframeid").src;

document.getElementById("iframeid").src;

回答by Lucas Green

Only two missing bits (marked with <== )

只有两个缺失位(用 <== 标记)

<html>
<head>
<script type="text/javascript">
        function load() {
            var url =  document.getElementById("iframeid").src; // <== .src

            //get the ifram onload url of href 
            //(i.e)   http://www.image.com/home.html     

        }
    </script>
</head>
<body>
<iframe onload="load()" name="Google"  id="iframeid"  scrolling="auto" style="width:95%;height:600px" src="http://www.image.com" > // <== Opening angle bracket

    </iframe>
</body>
</html>