用html显示JavaScript结果
Show JavaScript result with html
我有这个JavaScript代码,显示打开和关闭端口。我想在html页面上显示这段javaScript代码的结果,就像它在console.log中写的那样。
<html>
<head>
<script type="text/javascript">
var net = require('net');
var ipStart = 0;
var ipEnd = 245;
var timeout = 2000;
while (ipStart <= ipEnd) {
var count = ipStart;
(function(count) {
var hostOne = '192.168.0.' + count;
var hostTwo = '192.168.1.' + count;
var hostThree = '192.168.2.' + count;
var socket = new net.Socket();
socket.setTimeout(timeout, function() { socket.destroy(); });
socket.connect(80, function() {
console.log('IP Adress ' + hostOne + ' is on port 80 Open!');
console.log('IP Adress ' + hostTwo + ' is on port 80 Open!');
console.log('IP Adress ' + hostThree + ' is on port 80 Open!');
});
socket.on('error', function(e) {
console.log('IP Adress ' + hostOne + ' is on port 80 Closed!');
console.log('IP Adress ' + hostTwo + ' is on port 80 Closed!');
console.log('IP Adress ' + hostThree + ' is on port 80 Closed!');
});
})(count);
ipStart++;
}
</script>
</head>
<body>
</body>
</html>
当我只运行它的js文件时,这是终端上结果的一部分:
IP Adress 192.168.0.244 is on port 80 Closed!
IP Adress 192.168.1.244 is on port 80 Closed!
IP Adress 192.168.2.244 is on port 80 Closed!
IP Adress 192.168.0.245 is on port 80 Closed!
IP Adress 192.168.1.245 is on port 80 Closed!
IP Adress 192.168.2.245 is on port 80 Closed!
有人能帮忙吗?
Try This
<html>
<head>
</head>
<body>
<div id="contents"></div>
<script type="text/javascript">
var net = require('net');
var ipStart = 0;
var ipEnd = 245;
var timeout = 2000;
while (ipStart <= ipEnd) {
var count = ipStart;
(function(count) {
var hostOne = '192.168.0.' + count;
var hostTwo = '192.168.1.' + count;
var hostThree = '192.168.2.' + count;
var output ='';
var socket = new net.Socket();
socket.setTimeout(timeout, function() { socket.destroy(); });
socket.connect(80, function() {
output='IP Adress ' + hostOne + ' is on port 80 Open!';
output=output + 'IP Adress ' + hostTwo + ' is on port 80 Open!';
output=output + 'IP Adress ' + hostThree + ' is on port 80 Open!';
});
socket.on('error', function(e) {
output=output + 'IP Adress ' + hostOne + ' is on port 80 Closed!';
output=output + 'IP Adress ' + hostTwo + ' is on port 80 Closed!';
output=output + 'IP Adress ' + hostThree + ' is on port 80 Closed!';
});
})(count);
ipStart++;
}
getElementById('contents').innerHTML=output;
</script>
</body>
</html>
试试这个
document.getElementsByTagName('body')[0].innerHTML +="IP Adress "+hostOne+" is on port 80 Closed!"
但是如果window
对象不加载,它将抛出错误Cannot read property 'innerHTML' of undefined
。所以你要记住这个
相关文章:
- 奇怪的Javascript结果
- Javascript(jQuery)给了我奇怪的结果
- 数组删除重复结果Javascript
- 比较数组中的连续元素不会返回任何结果(javascript)
- split(' ') 函数没有给出正确的结果 Javascript
- 如何产生结果Javascript嵌套缺陷
- I'm试图使用onclick函数显示函数的结果.(javascript)
- 计算结果Javascript中不同数字的数量
- 以 HTML 格式显示计算结果 (javascript)
- 没有得到预期的结果 - javascript
- 从函数返回结果 - JavaScript
- 在一行中返回来自三元的结果(JavaScript)
- 骰子效果-如何在最后显示假结果和真结果(JavaScript)
- WebGL纹理加载结果(Javascript)类型错误
- 限制搜索结果Javascript
- IF语句多个答案-相同的结果Javascript
- 双异步循环,如何保证正确排序顺序的最终结果?(javascript)
- 添加图像到测验结果javascript
- 奇怪的结果javascript比较
- 如何改变iFrame结果JavaScript警告框