206

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?

1

5 Answers 5

320

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.

Sign up to request clarification or add additional context in comments.

5 Comments

how to set window's height and width to zero, if i set to zero it is showing full screen
how do we open it as a normal window rather than pop - up window? because unable to open new tab
@AkshathaSrinivas the minimum height is 100
Can I send some parameters to these page also how can I use them to new window?
@AshokkumarGanesan If you want it to open in a new tab in your browser then use: window.open("w3schools.com"). If you want to open it in the same tab then : window.open("w3schools.com", '_self');
88

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

6 Comments

shiplu.mokadd.im That seems to be what I need but I am not sure where it goes.
@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.
How is that the 4th parameter? It looks like the 3rd to me. Am I missing something?
@CoderDennis Nice catch. Fixed it.
hi how can I trigger the window close event?
|
16

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

1 Comment

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.
9

I know it's too late, but,

Steps for opening a link in a new tab:

  1. Create an a element
  2. Set the href ( "https://example.com" )
  3. Set target to "_blank"
  4. Click on the link

Code:

<button onclick="myFunction()">Open</button>
<script>
function myFunction() {
  var link = document.createElement("a")
  link.href = "https://example.com"
  link.target = "_blank"
  link.click()
}
</script>

3 Comments

This is probably the best answer around. Does not get caught to the popup blocking.
Beautiful, works like a complete charm. Thank you so much for this!
OP asking to open in a new window not in a new tab
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.