Starting from Ameer Dawood ideea of ping from JS ( pinger ), I’ve got a problem when the remote IP is a http server, and request HTTP authentification, the browser pop’s up the box for user/password. How can I avoid this, If the remote server request auth, we know that the remote server is responding and I need to call alive() function ( doesn’t work with http://someBogusUser:someBogusPass@ + ip, beacause it request the password again, I do not want to have the corect password saved on a client side script 🙂 )
The main part of the code is this:
img = new Image();
img.onload = function() { alive()();};
img.onerror = function() { unreachable()();};
img.src = "http://" + ip;
setTimeout(function() { unreachable();}, 1500);
Curently I’m trying to test if I can use a xmlhttprequest readyState and status to see if the remote IP is responding.
Anyone has an ideea on how to do this, or has knowledge of another method to test if a IP is alive from JS. Please don’t give me server side solutions (I’m using them now + AJAX).
Thank you
EDIT:
I think Web workers will stop any authentication popup from reaching the browser, but they can’t handle DOM events (onload, onerror) so workers can’t help … 🙁
I don’t think you can avoid it.
This hack is not ‘ping’ in any normal sense of the word; it won’t “check an IP is alive”. It’s an HTTP request and subject to authentication, redirection and other facets of HTTP. (And of course it won’t ‘ping’ a machine that’s not running an HTTP server.) Any method of remote inclusion (
img,script,style,iframe) risks popping up authentication.With XMLHttpRequest you will have problems because of the Same Origin Policy. IE and Opera will immediately fail any attempt with a security error. Firefox and WebKit will try to make the connection though you won’t get anything usable as a response so you’ll have to continue to guess from the timeout length. Which itself seems pretty problematic to me. Certainly it’s not out of bounds to expect the
/page of any given website to take more than 1.5s to return a complete document.In short I don’t think ‘pinger’ is viable as anything other than a fun experiment. For any real work you will have to continue with the server-side ping.