2

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.

Rahul Sudha
  • 137
  • 2
  • 7
  • Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – Thielicious Dec 25 '16 at 11:52
  • @Thielicious, But here i'm calling the URL value from a **javascript variable**, I'm not sure how to add this to the **window.open** thing... – Rahul Sudha Dec 25 '16 at 12:10
  • @ Rahul Sudha, this is the same thing, it's all explained in this duplicate even with variables. Check my answer – Thielicious Dec 25 '16 at 12:42

2 Answers2

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

Thielicious
  • 4,122
  • 2
  • 25
  • 35
0

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>
Daniyal Awan
  • 107
  • 5