Open a new tab with javascript but stay on current tab using javascript [duplicate]

巧了我就是萌 提交于 2019-12-30 03:47:06

问题


Is it possible to open a new tab in browser using the window.open("http://www.google.com"); function, but open it in the background and remain on the current page? When i Click in hyperlink the page will same but link will open in new tab...

I try this solution but it work only in Firefox link but I want to do in all browsers.


回答1:


As confirmed in both: source1 source2

there isn't a function that works throughout all browsers. There are options for popups, but this isn't a good idea as many use popup blockers.

To reiterate the first source, it's a browser setting for each user to decide to open a new tab in the background, or not. And because users decide this in their browser settings you will get inconsistent experiences.




回答2:


Try following may be helpful

<button id="open">open</button>

document.getElementById('open').onclick = function() {
    window.open('http://google.com');   
};

Note: You can't open tabs in the background using javascript because this is set in the user's preferences in about:config, which you have no control over. The setting in about:config in Firefox is:

It is only possible if you will be generate the Click event with Already Pressed Control Key Dynamically.

e.g. Ctrl + Click will always open new tab and stay you on current tab.

browser.tabs.loadDivertedInBackground=true




回答3:


Use window.open()

var win = window.open('http://stackoverflow.com/', '_blank');
if(!win)
{
 //Broswer has blocked it
 alert('Please allow popups for this site');
}

JSFilder




回答4:


try this code

 window.open(url,'_blank');


来源:https://stackoverflow.com/questions/20278498/open-a-new-tab-with-javascript-but-stay-on-current-tab-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!