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&amp;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');
Share:
526,085

Related videos on Youtube

Mark Mitchell
Author by

Mark Mitchell

Updated on January 25, 2022

Comments

  • Mark Mitchell
    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&amp;url=[sub]" 
       onclick="this.href = this.href.replace('[sub]',window.location)">
        LinkedIn
    </a>
    

    Any ideas?

  • Mark Mitchell
    Mark Mitchell over 11 years
    shiplu.mokadd.im That seems to be what I need but I am not sure where it goes.
  • John Dvorak
    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 the onclick. Using getElementById and addEventListener is cleaner still. Using jQuery to get a shorter syntax (and some other features + tons of plugins) is very popular as well.
  • CoderDennis
    CoderDennis over 10 years
    How is that the 4th parameter? It looks like the 3rd to me. Am I missing something?
  • Shiplu Mokaddim
    Shiplu Mokaddim over 10 years
    @CoderDennis Nice catch. Fixed it.
  • Dilakshan Sooriyanathan
    Dilakshan Sooriyanathan about 6 years
    hi how can I trigger the window close event?
  • Akshatha S R
    Akshatha S R almost 6 years
    how to set window's height and width to zero, if i set to zero it is showing full screen
  • brad
    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
    Ashok kumar Ganesan about 4 years
    how do we open it as a normal window rather than pop - up window? because unable to open new tab
  • Oscar Acevedo
    Oscar Acevedo about 4 years
    @AkshathaSrinivas the minimum height is 100
  • rahim.nagori
    rahim.nagori almost 4 years
    Can I send some parameters to these page also how can I use them to new window?
  • JohnK
    JohnK over 3 years
    I 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.