javascript 网址输入字段和打开该网址的提交按钮

javascript input field for web address and submit button to open that web address

我有一个网址输入字段,输入网址后,例如“yahoo.com”,我想要在新标签页或 window 中打开该地址的按钮.

<input type="text" width="30%" id="text" />
<input type="button" id="hlight2" value="Submit"  onClick="javascript: window.open();" />

像这样

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

更好(按回车键也会弹出 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>