Open URL in new window with JavaScript

JavascriptNew Window

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - 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.

Solution 2 - Javascript

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 - Javascript

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');

Solution 4 - Javascript

The following is JavaScript to be used in a function: Note, I have 1's and 0's instead of yes and no.

var theTop=((screen.height/2)-(theHeight/2))/2;
var theLeft=(screen.width/2)-(theWidth/2);
var features = 'height=600,width=800,top='+theTop+',left='+theLeft+',toolbar=1,Location=0,Directories=0,Status=0,menubar=1,Scrollbars=1,Resizable=1';

window.open(in_uri, WindowName, features);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark MitchellView Question on Stackoverflow
Solution 1 - JavascriptcitruspiView Answer on Stackoverflow
Solution 2 - JavascriptShiplu MokaddimView Answer on Stackoverflow
Solution 3 - JavascriptAnkit Kumar RajpootView Answer on Stackoverflow
Solution 4 - JavascriptJoe KView Answer on Stackoverflow