-1

I want to check a website that is not under my control that is it working or not when a visitor visit a page. I am an a platform where only HTML/CSS/JAVASCRIPT are allowed so no PHP etc.

Now my question is that is this possible via JavaScript to check a website is working or not? I saw many codes likes JQuery, AJAX but that are too complicated and I am new to coding. So can I do it via short and simple JavaScript?

halfer
  • 18,701
  • 13
  • 79
  • 158
  • 3
    What do you mean, "Now my question is that is this possible via JavaScript to check a website is working or not"? Please clarify to make it easier for people to answer your question and to prevent down-votes. If you need help, checkout the guide for asking a good question: http://stackoverflow.com/help/how-to-ask – Xanco Aug 20 '14 at 13:33
  • possible duplicate of [Is it possible to ping a server from Javascript?](http://stackoverflow.com/questions/4282151/is-it-possible-to-ping-a-server-from-javascript) – Nicolas R Aug 20 '14 at 13:36
  • @Xanco Before going to ask further help, I want to confirm that is my question right or wrong that Can JavaScript do it or not? –  Aug 20 '14 at 13:36
  • This question appears to be off-topic because it is about the capability of javascript!!!?! – Prasanth Aug 20 '14 at 13:39
  • 1
    @Mahendra What I 'think' you want is to check whether your sight is up or not... If so then yes, without using AJAX (which is a part of JavaScript as standard) Muhammad Hassan's answer is probably the best way to go, if you did want to use AJAX, then Anant Dabhi's answer is probably the best. But a question can't be wrong, but your question confused me as to what you wanted to achieve. The higher the clarity, then, (generally) higher quality answers you're going to get. – Xanco Aug 20 '14 at 13:43
  • @Xanco Thanks for mentioning. I also found **Muhammad Hassan** answer simple, short, best and working as I want to do. Next time I will consider the rules too. Thanks... –  Aug 20 '14 at 13:47
  • @Mahendra That's good to hear! Usually following the guidelines will result in clear and accurate answers. Have fun writing your websites! – Xanco Aug 20 '14 at 13:50

4 Answers4

0

Yes. Its possible to check that your desired website is working or not when anyone visit on your site. Here I am sharing a small pure JavaScript code with you.

<script type='text/javascript'>
// Check Server/Website Is Online Or Offline Via Pure JavaScript
// Shared On www.exeideas.com
var url = 'URL-OF-ANY-IMAGE-IN-YOURE-DESIRED-WEBSITE';
var img = new Image();
img.src = url;
img.onload = function() {
alert('WEBSITE IS UP AND RUNNING')
}
img.onerror = function() {
alert('WEBSITE IS DOWN AND NOT-RUNNING')
}
</script>

Rest you can know more about this code at Check Server/Website Is Online Or Offline Via Pure JavaScript.

Muhammad Hassan
  • 962
  • 3
  • 21
  • 45
0

This should do it. If you get response code 200 that means that site has responded, there might be redirect cases like 301 that are not covered by this answer but in your case they should not matter to much.

$.ajax({
  url: "test.html",
  statusCode: {
    200: function() {
      alert( "works" );
    }
  }
});
Matas Vaitkevicius
  • 49,230
  • 25
  • 212
  • 228
  • I can't see where the URL would go here. Moreover, won't this be prevented by the same-origin policy, if the remote domain is different to the local one? – halfer Aug 20 '14 at 13:47
  • @halfer I assumed you would know how to add url to ajax request. Now fixed. – Matas Vaitkevicius Aug 20 '14 at 13:49
  • I'm sure I could figure it out from the documentation, but it might as well be correct on this site. However, my other concern remains - if a remote site is put in the `url` string, will it work at all? – halfer Aug 20 '14 at 13:50
0
<html>
<head>
</head>
<body>
<script>

function checkServerStatus( url )
{
    var script = document.body.appendChild(document.createElement("script"));
    script.onload = function()
    {
        alert( url + " is online");
    }
    script.onerror = function()
    {
        alert( url + " is offline");
    };
    script.src = url;

}

checkServerStatus( "http://website" );


</script>
</body>
Zaraileto
  • 3
  • 1
0

you also can check it via jquery AJAX

here is example

<a href="/somelink" id="test1">Link1</a> <span id="result1"></span>
$.ajax($("#test1").attr("href"), {
  statusCode: {
    404: function() {
      $("#result1").html("not working");
    },
    200: function() {
      $("#result1").html("working");
    }
  }
});
Anant Dabhi
  • 9,506
  • 2
  • 28
  • 48