首页 > 解决方案 > onclick="window.open() 不适用于多个 URL

问题描述

var minutes = 0;
var seconds = 3;
url = 'https://www.google.com';
url2 = 'https://www.yahoo.com';
link_name = '<div class="j">Download File</div>'
var timer = setInterval(function () { myCounter() }, 1000);
function myCounter() {

  if (minutes > 59 || minutes < 0) {
    document.getElementById("time").innerHTML = "<font color='red'>Please check the minutes variable, Set it in between 0 to 59</font>";
  } else {
    seconds--;
    if (seconds == 0) {
      minutes = minutes - 1;
      seconds = 60;
    }
    document.getElementById("time").innerHTML = minutes + " Minutes " + seconds + " Seconds";
    if (minutes == -1) {
      clearTimeout(timer);
      document.getElementById("time").innerHTML = "<h3>Tile</h3>";
      document.getElementById("data").innerHTML = "<a href='" + url + "' onclick='window.open(" + url2 + ")'>" + link_name + "</a>";
    }
  }
}

嘿伙计们,如果单击“下载文件”,我想在主页选项卡中访问 google.com,在新选项卡中访问 yahoo.com。不要担心这个 Countdown javascript。只是我想修正这句话。document.getElementById("data").innerHTML="<a href='"+url+"' onclick='window.open("+url2+")'>"+link_name+"</a>";我尝试了很多次,但我只能访问 google.com。请帮我。

标签: javascripthtml

解决方案


您可以使用允许同时打开两个站点的功能。默认情况下你想要什么。否则,如果要在不同的选项卡中打开,则必须将第三个参数设置为 true。

// -- Function to visit two sites

function multi(url1, url2, twoTabs = false) {


    if(twoTabs) {

        window.open(url1);

    } else {

        window.location.href = url1;

    }

    window.open(url2);

}

// -- -- --

现在通常它应该工作。我添加了缺失的一分钟。

// -- Function to visit two sites

function multi(url1, url2, twoTabs = false) {


    if(twoTabs) {

        window.open(url1);

    } else {

        window.location.href = url1;

    }

    window.open(url2);

}

// -- -- --


var seconds = 3;
var minutes = 0;
url = 'https://www.google.com';
url2 = 'https://www.yahoo.com';
link_name = '<div class="j">Download File</div>'
var timer = setInterval(function () {
    myCounter()
}, 1000);

function myCounter() {

    if (minutes > 59 || minutes < 0) {
        document.getElementById("time").innerHTML = "<font color='red'>Please check the minutes variable, Set it in between 0 to 59</font>";
    } else {
        seconds--;
        if (seconds == 0) {
            minutes = minutes - 1;
            seconds = 60;
        }
        document.getElementById("time").innerHTML = minutes + " Minutes " + seconds + " Seconds";
        if (minutes == -1) {
            clearTimeout(timer);
            document.getElementById("time").innerHTML = "<h3>Tile</h3>";
            /* document.getElementById("data").innerHTML = "<a href='" + url + "' onclick='window.open(" + url2 + ")'>" + link_name + "</a>";*/
            document.getElementById("data").innerHTML = '<div onclick="multi(url, url2)">Clic</div>\n';
        }
    }
}


推荐阅读