首页 > 解决方案 > 如何在使用 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 但它仍然不起作用。任何想法我怎么能到达那里?

标签: javascriptfirefoxtabs

解决方案


发生这种情况是因为您编写的代码在前一页中运行。

因此,您将使用的任何函数都将在此 javascript 片段中编写和调用,并将在上一页中运行。

要解决此问题,您可以在打开的 url 的 javascript 部分添加确认功能。


推荐阅读