Skip to content

Instantly share code, notes, and snippets.

@agazso
Created March 3, 2011 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agazso/852070 to your computer and use it in GitHub Desktop.
Save agazso/852070 to your computer and use it in GitHub Desktop.
pinger.html
<html>
<script type="text/javascript">
function update(name, status, timeout)
{
var id = "ping:" + name.replace('/', ':');
var div = document.getElementById(id);
div.innerHTML = name + " is " + status;
var background = "white";
var color = "black";
if (status == "alive")
{
background = "green";
color = "white";
}
else if (status == "unreachable" || status == "timing out")
{
background = "red";
color = "white";
}
div.style.background = background;
div.style.color = color;
clearTimeout(timeout);
}
function pinger(url)
{
var div = url;
var img = new Image();
var timeout = setTimeout(function() {
update(div, "timing out", null);
img.complete = false;
}, 2000);
img.onload = function() { update(div, "alive", timeout); }
img.onerror = function() {
if (img.complete) update(div, "alive2", timeout);
else update(div, "unreachable", timeout);
}
img.src = "http://" + url + "/?" + new Date().getTime();
setTimeout(function() { pinger(url); }, 10000);
}
function start()
{
var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++)
{
var div = divs[i];
if (div.id.indexOf("ping:") == 0)
{
var url = div.id.replace(/ping:/, '');
div.innerHTML = url;
pinger(url);
}
}
}
</script>
<style>
* {
font-family: arial;
font-size: 20px;
}
div {
margin: 20px;
padding: 10px;
}
</style>
<body onload="start()">
<div id="ping:scalien.com"></div>
<div id="ping:mediafilter.hu"></div>
<div id="ping:94.76.202.195"></div>
<div id="ping:192.168.1.123"></div>
</body>
</html>
@Leurie02
Copy link

Hello? Can you refined it? It only gives out alive2 and when there is no internet it still gives me an alive2 response for few sec...

Thanks 😊😊😊

@agazso
Copy link
Author

agazso commented Oct 15, 2020

Sorry, it's not really possible with this approach to tell the difference...

@Leurie02
Copy link

Ohh? So it's not possible to check if there is Internet or not? How sad... I used it as an internet checker on my project but even no internet it gives me a reply alive2 for a few sec... Hehehe i thought it could give me an accurate reading if the internet is down or up... Hehehe thanks by the way...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment