使用 JQuery / Javascript 访问 iFrame URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access iFrame URL with JQuery / Javascript
提问by Jason
I have a page like this...
我有一个这样的页面...
<div>
<iframe id="mainContent">
<iframe id="littleBox">
</div>
and I want to do this...
我想这样做...
$(".someButton").live('click', function() {
alert(/*The URL That the Main Frame is ON*/);
});
I found this: $("#mainFrame").get(0).location.href, but it doesnt work...
我发现了这个:$("#mainFrame").get(0).location.href,但它不起作用......
Also, I need it to return the current url, so if someone navigates around it should return the current page they are on.
另外,我需要它返回当前的 url,所以如果有人在它周围导航,应该返回他们所在的当前页面。
回答by xar
Okay so you mean you have to access the URL of the parent from the child iframe?
好的,所以您的意思是您必须从子 iframe 访问父级的 URL?
$("#mainFrame").get(0).contentWindow.location
回答by Azmisov
Does iframe.src not work? Or, are you trying to get the parent document? In that case, you can just use parent.
iframe.src 不起作用吗?或者,您是否正在尝试获取父文档?在这种情况下,您可以只使用parent.
回答by JumpingJezza
As Azmisov says iframe.src does the trick
正如阿兹米索夫所说, iframe.src 可以解决问题
<head runat="server">
<title>iframe Test</title>
<script type="text/javascript">
function ShowURLs() {
var control = document.getElementById("mainContent");
alert("iframe source = " + control.src);
alert("this page = " + location.href);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="someButton" value="Click Me" onclick="ShowURLs()" />
<div>
<iframe id="mainContent" src="Test.aspx" />
<iframe id="littleBox" />
</div>
</div>
</form>
</body>
回答by JumpingJezza
document.getElementById('mainContent').contentWindow.location.href

