views:

87

answers:

4

I want to create a webpage in which I am adding some intranet links. I just wanted to know how to check whther the link that i have added, at a moment is working or not. I want to mark the link in RED if its not working else it should be green.

+3  A: 

If you want a lightweight programmatic check, you could do an HTTP HEAD request and check for a response code greater than or equal to 200 and less than 400.

Asaph
A: 

Ping something on the other side of the link. While you could probably find ways to check interface status or routing tables or various other things ... what you really want to know is whether the traffic is flowing to and from that destination.

On unix can use ping with the -c argument to send a number of packets, and -w to specify the wait time in seconds. Then check the exit status:

ping -c 3 -w 5 remote-host

ping will exit with non-zero exit code if packets are dropped.

Stef
@Stef ping response does not indicate if a web server is running let alone if the linked document exists on it. ping only proves a machine is reachable on a network.
Asaph
Ah, I thought by 'link' you meant network link. It wasn't clear to me that it was about a webpage link.
Stef
A: 

If you want to check client side...

<style type="text/css">
.good {
   color:green;
}

.bad {
   color:red;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"&gt;&lt;/script&gt;

<script type="text/javascript">
    $(document).ready(function(){
        $("a.checkLinks").each(checkme);
    })

    // load page from URL with ajax
    function checkme(i, item) {
        $.ajax({
            type: "GET",
            url: item.href,
            success: function(data, textStatus) {
                $(item).addClass("good");
            },
            error: function(request) {
                $(item).addClass("bad");
            }
          });
    }
</script>

<a class="checkLinks" href="good.html">Good</a><br/>
<a class="checkLinks" href="bad.html">Bad</a>
Lance Rushing
This won't actually work because of cross-domain restrictions
Jani Hartikainen
good point. correcting to assume same domain.
Lance Rushing
A: 

If you want check sever side (php example):

<?php

function isup($url) 
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    if(curl_exec($ch)) {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        return ($httpcode < 400);
    }

    return false;
}

$url = "http://example.com/404";

printf('<a href="%s" class="%s">Link</a>', $url, isup($url) ? 'good' : 'bad');
Lance Rushing