Javascript 从子窗口设置父窗口 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3553751/
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
Setting parent window URL from a child window?
提问by Kaddy
This is what I am trying to do: Once submit a form on my main JSP/HTML page a new page with some link opens. When I click on any of the link the link should open in the parent Window. I.e the Window I started from. How do i do this?
这就是我想要做的:一旦在我的主 JSP/HTML 页面上提交表单,就会打开一个带有一些链接的新页面。当我点击任何链接时,链接应该在父窗口中打开。即我开始的窗口。我该怎么做呢?
回答by Kranu
Use window.opener.location.href in javascript
在 javascript 中使用 window.opener.location.href
For example,
例如,
<a href="javascript:void(0);" onclick="window.opener.location.href='linkedFile.html';">Click me!</a>
回答by Pekka
You'll need JavaScript for this. HTML's targetcan't target the window's parent (opener) window.
为此,您将需要 JavaScript。HTMLtarget不能定位窗口的父(开启器)窗口。
The following will open the page in the parent window if JavaScript is enabled, and open it in a new window if it is disabled. Also, it will react gracefully if the current page does not have an opener window.
如果启用 JavaScript,以下将在父窗口中打开页面,如果禁用,则在新窗口中打开它。此外,如果当前页面没有打开窗口,它也会正常反应。
<a href="page.php"
onclick="if (typeof window.opener != 'undefined') // remove this line break
window.opener.location.href = this.href; return false;"
target="_blank">
Click
</a>
回答by Eli Grey
Use parent.location.hrefif it's on the same page in an iframe. Use opener.location.hrefif it's another entire tab/window.
使用parent.location.href如果是在同一页面上iframe中。使用opener.location.href如果它是另一个整个标签/窗口。

