JavaScript open a new tab but stay on the current page | Example Code

How to stay on current window when the link opens in new tab?

No, there a no way to stay on the current window instead of going to the tab because it is controlled by the browser.

Some basic about Window open method:-

window.open(url, "_self");
  1. _blank – URL is loaded into a new window. This is the default.
  2. _parent – URL is loaded into the parent frame.
  3. _self – URL replaces the current page.
  4. _top – URL replaces any framesets that may be loaded name – The name of the window.

Try JavaScript to open a new tab but stay on the current page

You can do popup new tab over the current page with less height and width.

<html>
<body>

	<script>
		myWindow=window.open('https://eyehunts.com/','','width=400,height=250')
		//myWindow.document.write("<p>This is 'myWindow'</p>")
		myWindow.focus()
	</script>

</body>
</html>

Output:

JavaScript open a new tab but stay on the current page

Open tabs in the background using JavaScript in not possible because this is set in the user’s preferences in about:config, which you have no control over. The setting is:

browser.tabs.loadDivertedInBackground=true

Do comment about this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.