Checking if a URL is broken in Javascript

Asked
Active3 hr before
Viewed126 times

7 Answers

checking
90%

I'd recommend to use jQuery for correct cross-browser ajax requests:, Is it fallacious to argue that something is correct, of good quality, or acceptable because a community of experts has established it as such? ,The simplest way to check if a URL is corrrect to send a http Head request. But how do you use that to specify the URL ?,Please be sure to answer the question. Provide details and share your research!

I'd recommend to use jQuery for correct cross-browser ajax requests:

function UrlExists(url, cb) {
   jQuery.ajax({
      url: url,
      dataType: 'text',
      type: 'GET',
      complete: function(xhr) {
         if (typeof cb === 'function')
            cb.apply(this, [xhr.status]);
      }
   });
}

Usage:

UrlExists('/path/script.pl', function(status) {
   if (status === 200) {
      // file was found
   } else if (status === 404) {
      // 404 not found
   }
});
load more v
88%

The simplest way to check if a URL is corrrect to send a http Head request. But how do you use that to specify the URL ?,I found this in a previous post :,This question has been posted on Stack before, but none so specific as to what I’m trying to understand.,I’d recommend to use jQuery for correct cross-browser ajax requests:

function UrlExists(url) {
   var http = new XMLHttpRequest();
   http.open('HEAD', url, false);
   http.send();
   return http.status != 404;
}
load more v
72%

There are quite a few options when it comes to checking links, and even more options if you’d rather use an application rather than JavaScript code. For this example, we’re going to use broken-link-checker, an NPM package found on GitHub.,After importing the SiteChecker class, we create a new instance of it with some configuration information. For this particular example, we’re saying that we want to include internal links as defined by the domain that we later plan to enqueue, as well as links that are not on our local domain. In this example, localhost links would be considered internal links.,There are various levels of filtering when it comes to links that should be checked. Per the documentation, the filter options are as follows:,Because this is a programming blog and there will likely be links inside code blocks, I didn’t want to chance any false positives. For this reason, I only wanted clickable links to be checked.

npm install broken - link - checker--save - dev
load more v
65%

As a quick experiment I wanted to see how much of a link checker I could build in JavaScript and run it from snippets.,When the link checker runs it shows me all the CSP errors in the console:,I’ve been experimenting more with JavaScript and working more from the console.,I knew this wouldn’t work for all links because the browser would block some of the requests due to cross site scripting concerns.

Which looks like this:

var links = document.querySelectorAll("a");
var linkReport = [];
links.forEach(function(link) {
   var reportLine = {
      url: link.getAttribute('href'),
      status: 0,
      message: "",
      element: link
   };
   linkReport.push(reportLine);
   // do stuff to the reportLine and link here
});
console.table(linkReport);
load more v
75%

option to run HtmlUrlChecker checks on page load (using jsdom) to include links added with JavaScript?,option to check if hashes exist in target URL document?,option to parse Markdown in HtmlChecker for links,Type: Number Default value: 1 The tags and attributes that are considered links for checking, split into the following levels:

npm install broken - link - checker - g
load more v
40%

This question has been posted on Stack before, but none so specific as to what I'm trying to understand.,The simplest way to check if a URL is corrrect to send a http Head request. But how do you use that to specify the URL ?,The result was the same. I got a simple page with the message:,onchange will work only if the value of the textbox changed compared to the value it had before, so for the first time it won't work because the state didn't change.

I found this in a previous post :

function UrlExists(url) {
   var http = new XMLHttpRequest();
   http.open('HEAD', url, false);
   http.send();
   return http.status != 404;
}
load more v
22%

Find out whether the link is valid or broken based on HTTP response code.,Repeat this for all the links captured.,Send HTTP request for the link and read HTTP response code.,Thus, we can obtain all links from web page and print whether links are valid or broken.

Below is the web driver code which tests our use case:

package automationPractice;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrokenLinks {
    
    private static WebDriver driver = null;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String homePage = "http://www.zlti.com";
        String url = "";
        HttpURLConnection huc = null;
        int respCode = 200;
        
        driver = new ChromeDriver();
        
        driver.manage().window().maximize();
        
        driver.get(homePage);
        
        List<WebElement> links = driver.findElements(By.tagName("a"));
        
        Iterator<WebElement> it = links.iterator();
        
        while(it.hasNext()){
            
            url = it.next().getAttribute("href");
            
            System.out.println(url);
        
            if(url == null || url.isEmpty()){
System.out.println("URL is either not configured for anchor tag or it is empty");
                continue;
            }
            
            if(!url.startsWith(homePage)){
                System.out.println("URL belongs to another domain, skipping it.");
                continue;
            }
            
            try {
                huc = (HttpURLConnection)(new URL(url).openConnection());
                
                huc.setRequestMethod("HEAD");
                
                huc.connect();
                
                respCode = huc.getResponseCode();
                
                if(respCode >= 400){
                    System.out.println(url+" is a broken link");
                }
                else{
                    System.out.println(url+" is a valid link");
                }
                    
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        driver.quit();

    }
}
load more v

Other "checking-undefined" queries related to "Checking if a URL is broken in Javascript"