2016-09-23 49 views
-1

我對js和很少的html一無所知,但發現並修改了此站點上的此位代碼,以便我們輕鬆地輸入我們的打印機主機名以獲取到他們的配置頁面。需要更改此工作代碼才能在新窗口中打開

我想嵌入到一個小工具,但需要它打開它自己的選項卡/窗口,如果我這樣做。我已經嘗試過並且一直無法找到執行此操作的代碼。

這裏是到目前爲止(工作)代碼:

<html> 
<body bgcolor=6D7E90> 
    <table id="content" height="30" border="0" cellspacing="0" cellpadding="0"> 
<tr><td> 
<script> 
function go(){ 
window.location='http://'+document.getElementById('url').value; 
} 
</script> 
<input type='text' id='url'> 
<button id='btn_go' onclick='javascript:go();'>Go</button> 
</td</tr> 
</table> 
</body> 
</html> 

回答

0

爲了幫助您與您的HTML/CSS,請參考下面的代碼。

注意事項/建議:

  • 使用DOCTYPE
  • window.location屬性將打開URL在同一窗口設置爲它
  • window.open方法打開與我們設定
  • 的URL新的瀏覽器窗口

代碼:

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Printer Hostnames</title> 

    <style> 
     body { 
      background-color: #6D7E90; 
     } 
    </style> 

    <script> 
     function go() { 
      var url = 'http://' + document.getElementById('url').value; 
      // open the url in a new browser window 
      window.open(url); 
     } 
    </script> 
</head> 
<body> 
    <table id="content" height="30" border="0" cellspacing="0" cellpadding="0"> 
     <tr> 
      <td> 
       <input type='text' id='url'> 
       <button id='btn_go' onclick='javascript:go();'>Go</button> 
      </td> 
     </tr> 
    </table> 
</body> 
</html> 
+0

這工作完美。謝謝! –

0

使用window.open

document.getElementById('btn_go').addEventListener('click', function() { 
    window.open('http://' + document.getElementById('url').value); 
}); 

https://jsfiddle.net/sg7pLcLL/

相關問題