Open Url in New Window with JavaScript

Open URL in new window with JavaScript

Use window.open():

<a onclick="window.open(document.URL, '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
Share Page
</a>

This will create a link titled Share Page which opens the current url in a new window with a height of 570 and width of 520.

Open a URL in a new tab (and not a new window)

Nothing an author can do can choose to open in a new tab instead of a new window; it is a user preference. (Note that the default user preference in most browsers is for new tabs, so a trivial test on a browser where that preference hasn't been changed will not demonstrate this.)

CSS3 proposed target-new, but the specification was abandoned.

The reverse is not true; by specifying certain window features for the window in the third argument of window.open(), you can trigger a new window when the preference is for tabs.

How to open a link in a new window?

This is how to open a link in a new window.

<a href="myLink" 
target="popup"
onclick="window.open('myLink','popup','width=600,height=600'); return false;">
Open Link in Popup
</a>

Always remember the sometimes that user configures their browsers to block all new tabs and windows (at least this is what I do), to avoid annoying advertisements and click baits links.

JavaScript open in a new window, not tab

Specify window "features" to the open call:

window.open(url, windowName, "height=200,width=200");

When you specify a width/height, it will open it in a new window instead of a tab.

See https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Position_and_size_features for all the possible features.



Related Topics



Leave a reply



Submit