2012-08-22 40 views
0

得到URL這是使用.............我想從IFRAME

我想從下面的代碼得到當前 URL(HREF)的代碼IM ... ............

<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> 
+0

當心還有「訪問跨源幀」錯誤。 –

回答

8

,如果他們在同一個域,協議和端口這隻會工作:

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

其他用途:

document.getElementById("iframe_id").src 
+1

我用這段代碼未定義的結果 – suryaguruprakash

+0

你能幫我嗎 – suryaguruprakash

+0

'document.getElementById(「iframe_id」)。src'將始終返回最初的uri ..所以不建議使用它。 –

2

試試這個:

document.getElementById("iframeid").src;

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> 
3

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> 
+0

我有undefined ...警告(console.log(frame.src)) – suryaguruprakash

+1

我已經嘗試過警報(frame.src)它給出http://www.image.com我想要導航菜單網址 – suryaguruprakash

+0

你能幫我嗎 – suryaguruprakash

1

嘗試。有時候,你不能接取的iframe網址

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