You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Could we add this snippet to easily spot dead links? It will send a HEAD to every link in the page and if it is not 200, 403, 405, it will append this to the link: "[status: statusCode]", it is all client side and isn't noticeable, not even in web pages with lots of links, its what I have on my website too. :)
// Function to check the status of a URL
function checkLinkStatus(link, callback) {
var proxyUrl = 'https://corsproxy.org/?';
var targetUrl = encodeURIComponent(link.href);
var xhr = new XMLHttpRequest();
xhr.open('HEAD', proxyUrl + targetUrl, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
callback(xhr.status);
}
};
xhr.send();
}
// Function to update link text with status code
function updateLinkText(link, statusCode) {
// Check if a status span already exists for this link
var existingStatusSpan = link.nextElementSibling;
if (existingStatusSpan && existingStatusSpan.classList.contains('status_code')) {
// Update the existing status span
existingStatusSpan.textContent = ' [status: ' + statusCode + ']';
} else {
// Create a new span element with the class 'status_code'
var statusSpan = document.createElement('span');
statusSpan.className = 'status_code';
// Check the status code and set the text content of the span
if (statusCode !== 200) {
if (statusCode !== 0) {
if (statusCode !== 403) {
if (statusCode !== 405) {
statusSpan.textContent = ' [status: ' + statusCode + ']';
}
}
}
}
// Insert the status span after the link
link.parentNode.insertBefore(statusSpan, link.nextSibling);
}
}
// Function to check all links on page load
function checkAllLinksOnPageLoad() {
var links = document.querySelectorAll('a');
links.forEach(function(link) {
checkLinkStatus(link, function(statusCode) {
updateLinkText(link, statusCode);
});
});
}
// Run the function when the page loads
window.onload = checkAllLinksOnPageLoad;
The text was updated successfully, but these errors were encountered:
Could we add this snippet to easily spot dead links? It will send a HEAD to every link in the page and if it is not 200, 403, 405, it will append this to the link: "[status: statusCode]", it is all client side and isn't noticeable, not even in web pages with lots of links, its what I have on my website too. :)
The text was updated successfully, but these errors were encountered: