用html显示JavaScript结果

时间:2014-07-19 10:28:12

标签: javascript html tags

我有这个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!

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

试试这个。

document.getElementsByTagName('body')[0].innerHTML +="IP Adress "+hostOne+" is on port 80 Closed!"

但是如果不加载window对象,它将抛出错误Cannot read property 'innerHTML' of undefined。所以你要牢记这一点。