-2

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>
user2636553
  • 11
  • 1
  • 1
  • 1
  • 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()](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) - manipulate DOM element content instead, like adding text to a div element. – doppelgreener Jul 31 '13 at 04: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 Jul 31 '13 at 04:44

2 Answers2

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

Community
  • 1
  • 1
Lonely
  • 543
  • 3
  • 5
  • 14
2

ping has a very specific meaning: https://tools.ietf.org/html/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

SheetJS
  • 20,261
  • 12
  • 60
  • 74