2015-10-06 96 views
0

我舉了一個例子。jQuery iframe - 如何在使用iframe時獲取父窗口的URL?

的test.html:

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> 
</head> 
<body> 
    <h1>main window test</h1> 
    <iframe src="./iframe.html"></iframe> 
</body> 
</html> 

Iframe.html的:

<!DOCTYPE html> 
    <html> 
    <head> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    </head> 
    <body> 
     <h1>iframe testing</h1> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       alert("window.location.href"+window.location.href); 
      }); 
     </script> 
     </body> 
    </html> 

在警報 - >我收到了iframe網址「C:\用戶\ nn96589 \桌面\ testfolder \ iframe.html「

但是,我需要主窗口的網址是」C:\ Users \ nc96589 \ Desktop \ testfolder \ test.html「

任何人都可以幫助我。 在此先感謝。

+2

退房:http://stackoverflow.com/questions/3420004/access-parent-url-from-iframe –

+0

如果你想要得到的主窗口的URL,你不應該嘗試在iframe.html中獲取URL。 window.location.href將始終獲取CURRENT FILE的URL。因此,如果你想獲得完整的URL,你將別無選擇,只能在test.html中提醒URL。 – jessica

回答

0

嘗試利用document.referrer

<iframe src="data:text/html,<!DOCTYPE html><html><body><script>document.write(document.referrer)</script></body></html>"> 
 
</iframe>

+0

感謝您的回答。但我不能觸摸主要的HTML頁面,因爲它來自第三方。我只能在頁面中使用iframe.html頁面和jquery。請幫我解決這個問題 –

+0

@VinayReddy _「無法觸及主頁面,因爲它來自第三方,我只能在該頁面中使用iframe.html頁面和jQuery。」_是的,試過'document.referrer '在'iframe.html'來檢索父窗口'url? – guest271314

+0

謝謝,你說得對。即使我想也一樣。但只是我正在嘗試,如果有任何可能的方式 –