Javascript ping

Home

Javascript ping

Sep212011

For work I needed to "ping" one website from another just using javascript - we have a customer on site A, and we want to send them to site B, but what if site B is broken? Let's send them somewhere else. So how do we tell if site B is up and running, and visible from where they are (in case, SAY, their DNS is broken). I found this and I adapted it to my needs like so:

var ping = {

preload:null,

timer:null,

ping:function( img, success, fail ) {

var sess = new Date();

var nocache = sess.getTime();

var uri = img+"?time="+nocache;

ping.preload = new Image();

ping.preload.onload = function() {

clearTimeout(ping.timer);

ping.timer = null;

success( img );

};

ping.preload.src = uri;

ping.timer = setTimeout( function ( ) {

clearTimeout( ping.timer );

ping.timer = null;

ping.preload = null;

fail( img );

}, 3000 );

},

};



then to use it

ping.ping( 'http://foo.com', success_function, fail_function );

so something like

ping.ping( 'http://foo.com/img.png',

function( img ) { alert( img + ' success' ) },

function( img ) { alert( img + ' failed' ) }
);


:: :: Comment / reply

Over to you, discussion about anything local, but please be considerate... Folkestone on TV, Kent gigs, Find a flatmate, rent a room, property queries, current news, what's new, local events, for sale, local services, what I like about Folkestone, ask questions, offer advice, plug yourself... the more it's used the more useful a resource it will become. If you want a gig listed click here to add it (fixed now!), or just click here to post a message...