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
(
afterif
". After fixing it, you would get "reference error:ping
is not defined". Oh, anddocument.write
cannot be used here and shouldn't be used. – John Dvorak Commented Jul 31, 2013 at 4:44
2 Answers
Reset to default 4You 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