-1

I have an input field for web address and once I put in the web address, example "yahoo.com", I'd like the button to open that address in new tab or window.

<input type="text" width="30%" id="text" />
<input type="button" id="hlight2" value="Submit"  onClick="javascript: window.open();" />
nin-j
  • 7
  • 4
  • Does this answer your question? [Open a URL in a new tab (and not a new window)](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window) You should be using an anchor tag instead of a button anyway – fogx Feb 20 '21 at 22:23
  • thank you, but @mplungjan answered my question ... i will check this out also though. – nin-j Feb 21 '21 at 04:48

1 Answers1

1

Like this

document.getElementById("hlight2").addEventListener("click",function() {
  const loc = document.getElementById("text").value;
  if (loc) window.open(loc,"_blank");
})

Better (pressing enter also pops the window)

document.getElementById("urlForm").addEventListener("submit", function(e) {
  e.preventDefault();
  const loc = document.getElementById("text").value;
  if (loc) window.open(loc, "_blank");
})
<form id="urlForm">
  <input type="text" width="30%" id="text" />
  <input type="submit" value="Submit" />
</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • oh hey thanks, but no server side, all local personal webpage i'm trying to create. i get "file:///home/chronos/u-xxxxxx/MyFiles/Downloads/www.yahoo.com" ... is there a way to open the link --> https://www.yahoo.com ? – nin-j Feb 20 '21 at 16:54
  • You need to add https:// – mplungjan Feb 20 '21 at 17:04
  • super. thank you! i modified and added "https://" --> window.open("https://"+loc, "_blank"); – nin-j Feb 20 '21 at 17:41