用html显示JavaScript结果

Show JavaScript result with html

本文关键字:结果 JavaScript 显示 html      更新时间:2023-09-26

我有这个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。所以你要记住这个