javascript - 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。请帮我。
解决方案
您可以使用允许同时打开两个站点的功能。默认情况下你想要什么。否则,如果要在不同的选项卡中打开,则必须将第三个参数设置为 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';
}
}
}
推荐阅读
- javascript - 如何使用Javascript更改元素的文本而不消失?
- html - 如何在 HTML 中创建嵌套表格
- c# - 如何在 ASP.NET Web 应用程序中只加载一次数据表中的静态数据
- swift - Swift 中 flatMap 的定义
- firebase - Flutter 和 Firebase:动态推送通知,例如 Whatsapp
- c++ - 与 asm volatile 相比,我们需要使用一些运行时内存围栏机制
- html - 如何让 div 部分覆盖其他样式代码?
- javascript - 他们是一种基于多边形属性禁用少数多边形交互的方法吗?
- google-apps-script - 谷歌表格 - 如何通过选择不同的单元格来触发一个或多个单元格的背景颜色变化?
- swift - 自定义合并 RateLimitedScheduler