I want the below button code to open a new tab with the URLfetched from a js variable

269 views Asked by At

can you please help me with the below query? I have a button code as shown below:

    <button type="button" onclick= "location.href=document.getElementById('Text1').value;return false;" class="btn btn-danger">Launch Console</button>

Here I have the URL value fetched from a javascript variable. Is there anyway I can open the same in a new tab ? Currently it's loading in the same window/tab.

2

There are 2 answers

3
Daniyal Awan On BEST ANSWER

You can do something like this :

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

<button type="button" onclick= "openInNewTab(urlHere)" class="btn btn-danger">Launch Console</button>
2
Thielicious On
<input id=text type=text>
<button onClick="window.open(document.getElementById('text').value, '_blank');">URL In New Tab</button>

Just like I said, read the duplicate closely. And don't use location.href.

DEMO