Skip to content Skip to sidebar Skip to footer

How To Test If Link On Page Works

I am trying to write a script that will test if all the anchor tags on a page are working. Essentially, I just want to make sure that the link is not broken. I thought that the bes

Solution 1:

google.com.ua doesn't allow make cross-origin requests, so that's why you get error. Try to make request to other domains. And yes, cross-origin must be enabled on them. This variant only for AJAX-requests.

I propose make something like ping in javascript. Working example you can find here - http://jsfiddle.net/GSSCD/203/

The main idea of example above - using images and their callback onload for detect if this domain is available.

P.S. I realize this AJAX-requests helper for me (can be helpful):

ajax: function(url, cb, data, method) {
        url = url ? url : '';
        cb = typeof cb == 'function' ? cb : function() {};
        data = data ? data : {};
        method = method ? method : 'GET';

        var request = new XMLHttpRequest();
        request.open(method, url, true);
        request.onload = function() {
            if (this.status >= 200 && this.status < 400) {
                cb.call(this, this.responseText);
            } else {
                cb.call(this);
            }
        };
        request.onerror = function() {
            cb.call(this);
        };
        request.send(this.getQueryUrl(data)); // You can pass just `null` to send

        return this;
    }

Solution 2:

You could also work directly with the exception thrown, using a try...catch block:

try {
    http.send();
    var resonse = http.status;
    console.log(response);
} catch (e) {
    console.log(e);
    // Do a fallback, call a failure callback, etc.
}

Solution 3:

Use AJAX to call your own local PHP file that uses cURL to check the links. I use this method myself with an array of all the websites that I currently manage (plus a few control URLs like Google) and it works great locally. Remotely, results may vary... I think certain hosts might not like certain other hosts, but I have yet to test that theory.

http://php.net/manual/en/function.curl-init.php

You can build it in whatever manner fits your needs, here is an explanation of how mine works.

  1. Build a javascript array of all URLs to test if the links are valid (working sites or down sites or broken links)

  2. Make an AJAX call for each item in the array to my PHP file ($_GET['url'])

  3. PHP file uses cURL to get the headers of the URL to be checked and returns the result in the format I like

  4. The results are processed, in my case I output the results into a list/structure and color code each result according to the HTTP response code.

If you are putting this code on a live site you may get varying results according to your host (I did). For my own purposes of checking that my own sites are up and running I use this code locally with XAMPP.


Solution 4:

Your browser will complain if you try to load a resource with XMLHttpRequest from a server that doesn't support CORS.

A possible work around is to pretend that the resource is an Image instead.

EDIT tried that, it didn't work (in Chrome) - downloading an HTML page into an <img> element caused the error event to fire instead of the load event and I can't see any way to differentiate that from the error event that's fired if the URL doesn't even exist :(


Post a Comment for "How To Test If Link On Page Works"