首页 > 解决方案 > 弹出窗口打开链接并自行关闭

问题描述

我设置了一个弹出窗口,为访问者提供两个链接的选择。如果单击任何一个链接,我希望(1)弹出窗口自行关闭,(2)所选链接在新窗口中打开。What occurs now when one of the two offered links is chosen is (1) the popup closes itself and (2) an empty new window opens. 如何让所选链接显示在新窗口中?谢谢你。

这是我写的代码:

在主窗口的 HEAD 中:

    function OpenQuick() 
    {
    var windowcontents = "https://myurl.com/quicklink.html";
    var win =window.open(windowcontents,"remote",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=450,height=425');
win.creator=self;
myWindow.focus();
}

在主窗口的正文中:

    <a href="javascript:OpenQuick();"><img src="https://www.myurl.com/images/link.gif"></a>

在弹出窗口中:

    <a href="choiceone.html" target="_blank" onclick="self.close();">First Choice</a>
    <a href="choicetwo.html" target="_blank" onclick="self.close();">Second Choice</a>    

标签: javascripthtml

解决方案


万一这可能会有所帮助。我不建议对 OpenQuick() 进行任何更改:

function OpenQuick() {
  var windowcontents = "https://myurl.com/quicklink.html";
  var win=window.open( windowcontents, "remote", "(whatever)");
  win.creator=self;
  myWindow.focus();
}
<a href="#" onclick="OpenQuick();"><img src="(someimage)"></a>

在新/弹出窗口中 -

    <a href="choiceone.html" target="anyname" onclick="newRoutine();">Choice1</a>
    <a href="choicetwo.html" target="anyname" onclick="newRoutine();">Choice2</a> 

在该弹出窗口上使用 javascript 定义 newRoutine -

function newRoutine () {
  setTimeout( function () { self.close(); }, 1000); // 1000 is in ms = 1s
}

这个想法是在尝试关闭之前给弹出窗口一些时间。


推荐阅读