javascript - 弹出窗口打开链接并自行关闭
问题描述
我设置了一个弹出窗口,为访问者提供两个链接的选择。如果单击任何一个链接,我希望(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>
解决方案
万一这可能会有所帮助。我不建议对 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
}
这个想法是在尝试关闭之前给弹出窗口一些时间。
推荐阅读
- events - 无论如何在提交事件中将 FormResponse 链接到电子表格中的目标行,反之亦然?
- swift - 将 Xcode 更新到 10.2.1 运行项目崩溃,出现问题 dyld:未加载库:@rpath/libswiftCore.dylib
- scala - 如何从spark中的字符串列中提取数字部分?
- reactjs - Provider.render():返回一个有效的 React 元素(或 null)
- java - 如何指定来自用户的输入?
- javascript - 无法打印对象的值
- javascript - 如何测试一个函数是否被等待,而不仅仅是被调用?
- php - Laravel 使用附加在数据透视表中包含一个附加字段
- apache-spark - Spark 结构化流式处理 Kafka 偏移管理
- openid - OpenId 是否应该隐藏 JWKS url?