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 の URL にアクセスできない場合がある

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

欠落している 2 ビットのみ ( <== でマーク)

<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 に答える