I guess target="_blank" would open new tab/Windows but will switch the tab as well, and no way I can find them stuff in html, Yes but when we click in link pressing control key it opens the link in new background tab, Using javascript we can stimulate same Here is code I found
function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = "http://www.google.com/";
var evt = document.createEvent("MouseEvents");
//the tenth parameter of initMouseEvent sets ctrl key
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,true, false, false, false, 0, null);
a.dispatchEvent(evt);
}
Answer from Adel Bachene on Stack OverflowI guess target="_blank" would open new tab/Windows but will switch the tab as well, and no way I can find them stuff in html, Yes but when we click in link pressing control key it opens the link in new background tab, Using javascript we can stimulate same Here is code I found
function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = "http://www.google.com/";
var evt = document.createEvent("MouseEvents");
//the tenth parameter of initMouseEvent sets ctrl key
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,true, false, false, false, 0, null);
a.dispatchEvent(evt);
}
<a href="www.stackoverflow.com" onclick="window.open('#','_blank');window.open(this.href,'_self');">
This will load the current web page in a new tab which the browser will focus on, and then load the href in the current tab
I dislike the fact that chrome automatically goes to the new tab. Is there a way to fix this?
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.
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
I'd like my links to open in a new tab using target="blank", but I'd like it to stay on the current page, rather than immediately switching tabs. Can someone help me do this? Please no answers like "Leave it up to your visitors." I have explicit reasons for my desires.
You can try using window.open to open the new window and returning false to prevent the default action.
<a href="http://example.com/" target="_blank" onclick="window.open( 'http://example.com/' ); return false;">Open New Link</a>
You can use this script. First, go to theme editor and find the footer.php file inside of the body section paste the script.
good luck
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
});
</script>