Open URL in new window with JavaScript
526,085
Solution 1
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.
Solution 2
Just use window.open()
function? The third parameter lets you specify window size.
Example
var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var URL = "https://www.linkedin.com/cws/share?mini=true&url=" + location.href;
var win = window.open(URL, "_blank", strWindowFeatures);
Solution 3
Don't confuse, if you won't give any strWindowFeatures then it will open in a new tab.
window.open('https://play.google.com/store/apps/details?id=com.drishya');
Related videos on Youtube
![Mark Mitchell](https://i.stack.imgur.com/Y1LQR.jpg?s=256&g=1)
Author by
Mark Mitchell
Updated on January 25, 2022Comments
-
Mark Mitchell over 2 years
I'm making a "share button" to share the current page. I would like to take the current page URL and open it in a new window. I have the current URL part working, but can't seem to get the next part working.
I'm struggling with the syntax. I would like to specify the new window size to
width=520, height=570
.Something like:
<a target="_blank" href="https://www.linkedin.com/cws/share?mini=true&url=[sub]" onclick="this.href = this.href.replace('[sub]',window.location)"> LinkedIn </a>
Any ideas?
-
Luke Alderton almost 9 yearspossible duplicate of Javascript - Open a given URL in a new tab by clicking a button
-
-
Mark Mitchell over 11 yearsshiplu.mokadd.im That seems to be what I need but I am not sure where it goes.
-
John Dvorak over 11 years@MarkMitchell If you don't care about coding standards - into the
onclick
attribute. A slightly better option is to create a function that you call from theonclick
. UsinggetElementById
andaddEventListener
is cleaner still. Using jQuery to get a shorter syntax (and some other features + tons of plugins) is very popular as well. -
CoderDennis over 10 yearsHow is that the 4th parameter? It looks like the 3rd to me. Am I missing something?
-
Shiplu Mokaddim over 10 years@CoderDennis Nice catch. Fixed it.
-
Dilakshan Sooriyanathan about 6 yearshi how can I trigger the window close event?
-
Akshatha S R almost 6 yearshow to set window's height and width to zero, if i set to zero it is showing full screen
-
brad over 5 years@DilakshanSooriyanathan you can close the opened window using the variable that you gave to the new window. The code above would use
win.close()
. -
Ashok kumar Ganesan about 4 yearshow do we open it as a normal window rather than pop - up window? because unable to open new tab
-
Oscar Acevedo about 4 years@AkshathaSrinivas the minimum height is 100
-
rahim.nagori almost 4 yearsCan I send some parameters to these page also how can I use them to new window?
-
JohnK over 3 yearsI don't think that's true. At least in the Linux Chrome I'm using, the same
window.open(url, '_blank')
seems to decide whether to open a new window or a new tab based on whether I hold down the shift or control key, respectively, with my click.