Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Open a given URL in a new tab by clicking a button

Tags:

javascript

I would like to have a button that redirects to a given URL and opens in a new tab. How can this be done?

like image 266
wong2 Avatar asked Jun 10 '11 08:06

wong2


People also ask

How can you open an URL into a new tab when clicked?

Method 1: Ctrl+Click The first method requires a keyboard and a mouse or trackpad. Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.

Can you make the link open in a new tab JavaScript?

You need to use the _blank value in the target attribute to open the linked URL in a new tab or window. If you want to open URL with JavaScript, the open() method of Window interface is the best option. The JavaScript window. open() method opens a new browser window.

How can you force the browser to open a link in a new window or tab?

To open a link in a new browser window, hold the Shift on then click the link or right-click the link and select Open link in New Window.


2 Answers

Use this:

<input type="button" value="button name" onclick="window.open('http://www.website.com/page')" /> 

Worked for me and it will open an actual new 'popup' window rather than a new full browser or tab. You can also add variables to it to stop it from showing specific browser traits as follows:

onclick="window.open(this.href,'popUpWindow','height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no'); return false;" 
like image 155
Luke Alderton Avatar answered Sep 23 '22 17:09

Luke Alderton


In javascript you can do:

window.open(url, "_blank"); 
like image 29
satnam Avatar answered Sep 20 '22 17:09

satnam