Javascript Safari中具有当前页面重定向的新选项卡/窗口链接
我这里有一个脚本,它的主要目的是在一个新的选项卡/窗口中打开一个指定的链接,然后重定向当前的窗口(在这里启动了脚本,而不是新的选项卡) 它的工作原理如下。您指定了一个链接来侦听单击事件,单击该链接后,将触发两条指令。应打开新选项卡,并重定向当前窗口 问题是该脚本在chrome中工作,但在safari中,该脚本只会重定向到指定的url,新的选项卡/窗口不会打开Javascript Safari中具有当前页面重定向的新选项卡/窗口链接,javascript,Javascript,我这里有一个脚本,它的主要目的是在一个新的选项卡/窗口中打开一个指定的链接,然后重定向当前的窗口(在这里启动了脚本,而不是新的选项卡) 它的工作原理如下。您指定了一个链接来侦听单击事件,单击该链接后,将触发两条指令。应打开新选项卡,并重定向当前窗口 问题是该脚本在chrome中工作,但在safari中,该脚本只会重定向到指定的url,新的选项卡/窗口不会打开 function openTab(url) { // Create link in memory var a = wind
function openTab(url) {
// Create link in memory
var a = window.document.createElement("a");
a.target = '_blank';
a.href = url;
// Dispatch fake click
var e = window.document.createEvent("MouseEvents");
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
var aNode = document.getElementsByTagName('a'),
/* this is current link's url, just click copy link address you want to affect, and paste it here between ''*/
/* it supports multiple buttons with different links */
currentUrl = ['https://www.google.com/intl/en/ads/'],
/*this link loads in new tab */
newTabUrl = 'http://www.google.com/',
/* this link loads after the 'newTabUrl' is opened */
redirectUrl = 'http://www.youtube.com/';
for (var t = 0; currentUrl["length"] > t; t++) {
for (i in aNode) {
if (aNode[i].href && aNode[i].href == currentUrl[t]) {
aNode[i].href = redirectUrl; // without this the page will be not redirected in chrome (second instruction in the addEventListener, function below does not fire up somehow...)
aNode[i].addEventListener('click', function(){
openTab(newTabUrl);
window.location = redirectUrl;
console.log('fired'); //debugging
});
console.log('affected!'); //debugging
}
console.log(i); //debugging
}
}
这是一个完全正确的问题,但如果用户不希望出现这种行为,则可能会让用户感到不安。除非您提到页面按钮下的新选项卡。这是真的-那么他们会期待的!我必须提到,openTab函数在Safari中工作,但在上面的脚本中,它不知何故没有启动。但奇怪的是,下一条指令(openTab(newTabUrl)下面)确实启动了