7

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

我想从下面的代码中获取当前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>
4

5 回答 5

21

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

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

否则使用:

document.getElementById("iframe_id").src
于 2012-08-22T08:33:50.513 回答
5

演示。

<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>
于 2012-08-22T08:32:13.383 回答
3

试试这个:

document.getElementById("iframeid").src;

于 2012-08-22T08:31:16.113 回答
2

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

var CurrentUrl = document.getElementById('MyIFrame').contentWindow. location.href;
于 2012-08-22T08:33:47.047 回答
1

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

<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>
于 2012-08-22T08:32:05.677 回答