如何从弹出窗口将主窗口重定向到URL?

3

我有一个带有表单的弹出窗口。在提交表单时,我希望重定向到特定页面,但是在父窗口中(而不是在弹出窗口中)。

如何使用Javascript实现这一点?

After Application of Josh Idea

我正在调用一个JavaScript函数来提交表单,在这个JavaScript中,下面是所提到的代码。
所以,我试过了这个,但它并没有按照我的需求工作。
function instant_popup_post()
{
    var cid         = document.getElementById('product').value;
    var session_id  = document.getElementById('sessid').value;
    if(cid==30)
    {
        alert(document.getElementById('instantpop').onsubmit="opener.location.href = 'http://192.168.1.5/cppl11/bannerbuzznew/full_color_banner.php?&id=+cid+'&info_id=5&osCsid='+session_id;");
        document.instantpop.submit();   
    }
    else if(cid==31)
    {
        document.getElementById('instantpop').onsubmit="opener.location.href ='perforated_window_signs.php?&id='+cid+'&info_id=6&osCsid='+session_id;";
        document.instantpop.submit();   
    }
    else if(cid==32)
    {
        document.getElementById('instantpop').onsubmit="opener.location.href ='preprinted_stock_banner.php?&id='+cid+'&info_id=7&osCsid='+session_id;";
        document.instantpop.submit();
    }   
}

plss help


我解决了它 Josh,再次感谢您。 - OM The Eternity
2个回答

4

在弹出窗口中,您可以使用 opener 属性引用父窗口...

opener.location.href = 'http://www.google.com';

你可以在父窗口上调用函数...
opener.functionName();

当然,这里适用于良好的同源策略限制。

你能否更详细地解释一下window.opener,我无法完全理解你提供的链接。 - OM The Eternity
@OM 尝试一些快速而简单的东西,比如 <form onsubmit="opener.location.href = 'http://www.google.com';"> - Josh Stodola

0
我建议使用showModalDialog,这样您就可以冻结父窗口,在完成后,您可以向父窗口发送变量并进行重定向:
主窗口:
function ShowModalForm()
{
    var urlToRedirect = window.showModalDialog('url');
    if (urlToRedirect) 
       window.location = urlToRedirect;
}

弹出窗口:

function buttonAcceptClicked()
{
    //Do stuff you need
    window.returnValue = "new url";
    window.close()
}

这里有很多关于这个的信息。


你能展示一些这个东西的应用吗? - OM The Eternity
@OM The Eternity,Modal popups的想法是在打开时停止“父线程”,因此如果你在modal中有一个表单,父执行将暂停在window.showModalDialog行,直到该模态关闭,之后它将继续使用返回的变量。 - pjnovas
@Josh 我同意,那只能在IE上运行。@OM 在开始之前要考虑这一点,呵呵。 - pjnovas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接