需要修改此工作代码以在新窗口/选项卡中打开
Need this working code modified to open in a new window/tab
我对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/相关文章:
- 激活镀铬窗口选项卡
- 检测内容'It’’’’我在窗口选项卡上
- 如何通过单击Highchart图中的列/栏打开新窗口/选项卡
- Javascript表单提交打开新窗口选项卡,然后重定向父页面
- 来自 HREF 的新窗口/选项卡
- 检查锚点打开的窗口/选项卡是否已关闭
- 离开窗口选项卡时堆叠(重叠)的图像
- 如何从JavaScript弹出窗口打开父窗口选项卡
- 将表单提交到新窗口/选项卡
- 如何在新窗口/选项卡中打开文本和 xml 文件
- 检测链接是在新窗口/选项卡还是当前窗口中打开
- 在c中按下一个按钮,打开两个新的窗口/选项卡(不同的外部站点)
- 新窗口/选项卡中的JavaScript确认对话框
- 如何在浏览器窗口/选项卡关闭时删除本地存储项目
- 如何打开新窗口/选项卡,但专注于打开窗口
- 打开一个新窗口/选项卡
- Chrome窗口不会从无限滚动加载内容,除非窗口(选项卡)处于焦点位置
- JQuery发送带有Headers的Ajax GET,并在新窗口/选项卡中打开
- 如何防止firefox内部的javascript关闭窗口/选项卡
- 关闭窗口/选项卡时的Websocket-Chrome