我舉了一個例子。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「
任何人都可以幫助我。 在此先感謝。
退房:http://stackoverflow.com/questions/3420004/access-parent-url-from-iframe –
如果你想要得到的主窗口的URL,你不應該嘗試在iframe.html中獲取URL。 window.location.href將始終獲取CURRENT FILE的URL。因此,如果你想獲得完整的URL,你將別無選擇,只能在test.html中提醒URL。 – jessica