最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ping list of ip addresses using Javascript - Stack Overflow

programmeradmin0浏览0评论

I have a list of ip addresses to ping and would like to use Java script to do that.

I tried as the following but there is no output when I clicked the "Test" button.

<!DOCTYPE html>
<html>
<head>
<title>Test</title>

<script>
function myFunction()
{
var myStringArray = [ "10.100.200.133", "10.10.22.0" ];
len=myStringArray.length
for (var i=0; len=myStringArray.length; i<len; i++) {
    check=ping(i)
    if $check Then
        document.write("ip"+ i + "is up")
    else
        document.write("ip"+ i + "is down")
}
}
</script>
</head>

<body>
<button onclick="myFunction()">Test</button>
</body>
</html>

I have a list of ip addresses to ping and would like to use Java script to do that.

I tried as the following but there is no output when I clicked the "Test" button.

<!DOCTYPE html>
<html>
<head>
<title>Test</title>

<script>
function myFunction()
{
var myStringArray = [ "10.100.200.133", "10.10.22.0" ];
len=myStringArray.length
for (var i=0; len=myStringArray.length; i<len; i++) {
    check=ping(i)
    if $check Then
        document.write("ip"+ i + "is up")
    else
        document.write("ip"+ i + "is down")
}
}
</script>
</head>

<body>
<button onclick="myFunction()">Test</button>
</body>
</html>
Share Improve this question asked Jul 31, 2013 at 4:15 user2636553user2636553 111 gold badge1 silver badge1 bronze badge 2
  • 3 So what's the question? Are you asking why it doesn't work? You don't appear to have a ping function defined anywhere - maybe that has something to do with it. Also, don't use document.write() - manipulate DOM element content instead, like adding text to a div element. – doppelgreener Commented Jul 31, 2013 at 4:18
  • Did you open the debugging console? If you did, you would have surely seen an error message basically saying. "syntax error: expected ( after if". After fixing it, you would get "reference error: ping is not defined". Oh, and document.write cannot be used here and shouldn't be used. – John Dvorak Commented Jul 31, 2013 at 4:44
Add a ment  | 

2 Answers 2

Reset to default 4

You can't directly "ping" in JavaScript. There may be a few other ways:

  • Ajax

  • Using a Java applet with isReachable

  • Writing a server-side script which pings, and using AJAX to municate to your server-side script

  • You might also be able to ping in Flash (using ActionScript)

  • There are also some methods for pinging in JavaScript described in this question: Is it possible to ping a server from Javascript?

ping has a very specific meaning: https://www.rfc-editor/rfc/rfc1122#section-3.2.2.6, which cannot be replicated using a browser API.

If you want to just test if a web page hosted at a particular IP is available, you can use XMLHttpRequest

发布评论

评论列表(0)

  1. 暂无评论