javascript - 如何在使用 window.open 打开的新选项卡中创建提示
问题描述
代码:
button = document.getElementById('button');
function buttonPress(){
input = document.getElementById('input');
url = input.value;
console.log(url);
let a = window.open(url,"name");
a.focus();
setTimeout(console.log("wait until confirm"),4000);
if(confirm('Start download?')){
alert('Downloading');
}
else{
alert('Why you no love me...');
window.close;
}
}
button.addEventListener("click",buttonPress)
问题是确认在上一个选项卡中打开,而不是在新选项卡中打开。我已经尝试使用 location.replace 或 href 但它仍然不起作用。任何想法我怎么能到达那里?
解决方案
发生这种情况是因为您编写的代码在前一页中运行。
因此,您将使用的任何函数都将在此 javascript 片段中编写和调用,并将在上一页中运行。
要解决此问题,您可以在打开的 url 的 javascript 部分添加确认功能。
推荐阅读
- r - 如何只保留R中时差小于3分钟的事件
- python - 带有 PyQt 和 Python 线程、PySerial 和 GPIO 的网络摄像头流媒体
- javascript - “HTMLElement”类型上不存在属性“pseudoStyle”
- vbscript - AddField 函数无法按预期工作
- jenkins - 运行 Jenkins 测试时无法监听端口 33848
- playframework - 如何在 ubuntu 中安装 playframework 2.6?
- css - 100% 宽度的标签?
- apache-spark - 多个 Spark 提交作业错误
- asp.net-mvc - ADAL ASP.NET MVC 和 Angular。从 ASP .NET MVC 登录
- gulp - 使用 gulp-usemin 为每个文件夹制作 index.html