Ajax Project - Link Checker

Didn't find what you want? Try our search

This little project is a slightly different twist on what we can do with Ajax.

Wouldn't it be great if your links were automatically checked each time anyone visited your page? Because Ajax allows us to make a server call without an associated page load/refresh cycle, we can write a routine to list all the links on a web page and send that list to a server-side script which will check them all, and report any broken ones to the webmaster.

Provided that our script is quite efficient, and the pages to be checked don't contain huge numbers of links, our user should never know anything's happening!

The complete JavaScript code to be put into each page to be checked is listed below:

<script Language="JavaScript"> function getXMLHTTPRequest() { try { req = new XMLHttpRequest(); } catch(err1) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (err2) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (err3) { req = false; } } } return req; } var http = getXMLHTTPRequest(); function checklinks() { var txt = ''; var j = document.getElementsByTagName('a').length; for(var i = 0; i < (j-1); i++) // iterate through the links { lnk = document.getElementsByTagName('a')[i]; txt = txt + lnk.href + "|"; // build the link list } lnk = document.getElementsByTagName('a')[j-1]; txt = txt + lnk.href; // no divider after last one var myurl = 'linkchecker.php?list='; myRand = parseInt(Math.random()*999999999999999); var modurl = myurl+txt+"&rand="+myRand; http.open("GET", modurl, true); http.onreadystatechange = useHttpResponse; http.send(null); } function useHttpResponse() { if (http.readyState == 4) { if(http.status != 200) { alert('Link Check Problem'); } } } </script>

Most of this code will already be familiar to you, especially the function getXMLHTTPRequest() and the general style of the callback function useHttpResponse(). (If not, please have a look at around this site for tutorials and example code).

The important part of this application is the function checklinks(). This function collects all the page's link elements into an array by using JavaScript's getElementsByTagName() method. It then uses the targets of these links to build a string of URLs, each separated by the | character. This string is then appended to the server URL as part of a parameter/value pair:

list=url1|url2|url3|url4

Now let's consider what happens in the server script:

$email = "webmaster@*******.net"; // change to suit $link = explode("|",$list); // put URLs into an array $badcount = 0; $out = "Link Check For: ".$_SERVER['HTTP_REFERER'] ."\n\n".date("F j, Y, g:i a")."\n\n"; foreach($link as $url) { $fp = @fopen($url, "r"); if (!$fp) { $out .= "Link $url is dead!\n"; $badcount++; } } if($badcount > 0) mail($email,"Link Check: $url",$out,"From:AjaxLinkCheck");

In PHP we 'explode' this list into a PHP array containing the URLs and check them one by one, appending a list each time we find a broken one.

After checking them all, if the count of bad links is greater than zero, we mail the webmaster with a list of all the bad ones, using the PHP mail() method.

On this occasion, we don't really need any feedback from the server. In the example above, the callback function reports a problem (via a JavaScript alert) if the server call does not complete properly. In practice, though, we would no doubt prefer that the user not be made aware of the program's function at all.

By including all of the JavaScript into an external file checklinks.js, all we would need to do is include that file into the section of any page we want checked:

<script type="text/javascript" src="checklinks.js"></script>

... add onLoad="checklinks()" to the body tag of the page, and then upload our server-side script, configured with the correct email address. Done!

Please note that this is simplified code for study purposes and would need a little modification to work well in practice. In particular, we are simply checking the URLs by using @fopen(); a much better approach would be to use a HTTP HEAD request and collect the server status code.


Before using any software from this site, please see the Terms



6 Users Online.

All of the code and information on this site is provided free of charge (but without warranty).
If you feel that the site has been useful, you may care to donate a little toward the running of the site and the development of further projects?