![](/img/trans.png)
[英]Popup comes, ask me to redirect to external URL when the link opening in new tab in my WordPress website
[英]Opening new tab when clicking on a link in my website keeping the current tab active
我有這段代碼可以滿足我的需求。 它會在另一個選項卡中打開鏈接,但會移動到新打開的選項卡,而使我停留在當前選項卡中。我希望它保持當前選項卡處於活動狀態,而新打開的選項卡才剛剛打開。
function loadpopunder() {
var clicked = false;
$('a').each(function() {
this.onclick = function() {
var a = document.createElement("a");
a.href = "http://sitetopopeninnewtab.com";
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
a.target = "_blank";
}
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
// var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, });
var evt = document.createEvent("MouseEvents");
}
else {
// var evt = new MouseEvent('click', {'view': window, 'bubbles': true, 'cancelable': true, 'metaKey': true, 'ctrlKey': true});
var evt = document.createEvent("MouseEvents");
}
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
true, false, false, false, 0, null);
if (clicked == false) {
a.dispatchEvent(evt);
clicked = true;
}
}
});
if(window.location.href == 'http://mymainwebsite.com/'){
loadpopunder();
}
無法正確完成此操作,因為您取決於瀏覽器設置。 即使使用target='_blank'
在新標簽頁中打開url,如果將瀏覽器設置為在彈出窗口中打開新頁面,也不會獲得新標簽頁。
您可以在評論中看到所有瀏覽器都有不同的行為。 但是,您可以使用javascript window
對象管理新打開的窗口
您可以做的另一件事是使用javascript 可見性api檢查窗口是否聚焦。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.