How to open a new window using a HTML button

By   01-18-2015 · 1 minute read · 97 words


Recently I struggled figuring out how to make a HTML button tag open a new window when the button link was clicked. My original button only opened a link within the same window. What I wanted to achieve was getting the button to open a separate window and keep everything using HTML without the need to bring in javascript.

With the following snippet of code:

<button onClick="location.href='http://google.com'" type="button">Click me</button>

The button simply uses the same tab to visit google.com.

The solution was to use:

<button onClick="window.open('http://google.com')">Open second window!</button>

Which now opens a new browser window once clicked.

html  

comments powered by Disqus