需要修改此工作代码以在新窗口/选项卡中打开

Need this working code modified to open in a new window/tab

本文关键字:窗口 选项 修改 工作 代码 新窗口      更新时间:2023-09-26

我对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>

为了帮助你的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>

您应该使用new Window而不是Window .location。https://developer.mozilla.org/en-US/docs/Web/API/Window/open

使用window.open:

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