Skip to main content

Webpage Testing

Comments

1 comment

  • Delbra Brown

    They created an extension called Link Checker for me. If you copy and paste the information below into an extension, add it as a step to a journey then execute it, it will check all of the URL's on the page. You have to view the results to see what URL's aren't valid but it works great

     

    // How to check all the links in the webpage

    /* Function to check all the links in the webpage */


     

     /*   var links = document.getElementsByTagName('a');

        var result = [];

        for (var i = 0; i < links.length; i++) {

            result.push(links[i].href);

        }

        return result;

    /* Example usage */

    /*const anchors = document.getElementsByTagName('a');



     

        // Loop through each anchor element

        for (let i = 0; i < anchors.length; i++) {

            const anchor = anchors[i];

            const href = anchor.href;


     

            // Make an HTTP HEAD request to check if the link is valid

            fetch(href, {

                    method: 'HEAD'

                })

                .then(response => {

                    if (response.ok) {

                        return (`Link ${href} is valid`);

                    } else {

                        return (`Link ${href} is broken`);

                    }

                })

                .catch(error => {

                    console.error(

                        `Error checking link ${href}: ${error}`);

                });

        }*/


     

        // javascript which Validates that links are active and accessible

    /* Function to validate active and accessible links */

    function validateLinks() {

        var links = document.getElementsByTagName('a');

        var results = [];


     

        for (var i = 0; i < links.length; i++) {

            var link = links[i];

            var href = link.getAttribute('href');


     

            if (href.startsWith('http') || href.startsWith('https')) {

                var xhr = new XMLHttpRequest();

                xhr.open('HEAD', href, false);

                xhr.send();


     

                if (xhr.status === 200) {

                    results.push({

                        link: href,

                        status: 'active'

                    });

                } else {

                    results.push({

                        link: href,

                        status: 'inactive'

                    });

                }

            } else {

                results.push({

                    link: href,

                    status: 'invalid'

                });

            }

        }


     

        return results;

    }


     

    /* Example usage */

    var linkResults = validateLinks();

    return (linkResults);

    /* Output: Array of objects containing link and status */

    /* External library used: None */

    1

Please sign in to leave a comment.