Javascript 如何关闭弹出窗口并重定向页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7552726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:41:54  来源:igfitidea点击:

How to close popup and redirect a page

javascriptredirectpopup

提问by Kasim

My site using php and i create an online quiz and the quiz show in pop up mode.
Currently when i want to redirect my page using the script below. it goes to my homepage BUT it still at the pop up mode. I want it to close the pop up and go to homepage after I click finish.

我的网站使用 php,我创建了一个在线测验,测验以弹出模式显示。
目前,当我想使用下面的脚本重定向我的页面时。它转到我的主页,但它仍处于弹出模式。我希望它在单击完成后关闭弹出窗口并转到主页。

How can I do that?

我怎样才能做到这一点?

<script type="text/javascript">

window.parent.location = "../../../"

</script>

回答by Xavi López

You can access the window that opened a popup with the window.openerproperty on the popup. So, something like this should work:

您可以使用弹出窗口上的window.opener属性访问打开弹出窗口的窗口。所以,这样的事情应该工作:

window.opener.location.replace(redirectUrl);
window.close;

If you wanted to put that behavior in the onclickevent on a submit button you're building like this:

如果您想将该行为onclick放在提交按钮上的事件中,您可以像这样构建:

echo "<input type=\"submit\" 
       name=\"finishattempt\" 
       value=\"".get_string("finishattempt", "quiz")."\" 
       onclick=\"$onclick\" />\n";

You'd need to assign the String window.opener.location.href='someUrl';window.close();to the variable $onclickbefore echoing the submit button out.

在回显提交按钮之前,您需要将字符串分配给window.opener.location.href='someUrl';window.close();变量$onclick

回答by JMax

You can try this code (used on an HTML button):

你可以试试这个代码(用在 HTML 按钮上):

<input type="button" onclick="parent.window.opener.location='http://homepage.com'; window.close();">

And have a look at some similar threads like this one: Javascript: Close Popup, Open New Window & Redirect

并查看一些类似的线程:Javascript: Close Popup, Open New Window & Redirect

[EDIT] See this articletoo

[编辑]参见这篇文章

回答by Rukmi Patel

this way you can do it ..

这样你就可以做到..

<script type="text/javascript">
function close_window(){
    window.opener.location = 'pop_up.php';
    window.close();
}
</script>

html code..

html代码..

<body>
<form method="post" name="frm_2" id="frm_2">
    <input type="submit" name="btn_close" id="btn_close" onclick="return close_window();" />
</form>
</body>