Broken Link Checker
Broken Link Checker
Find and analyze links in your webpage HTML.
/* ========================================= TAB SWITCH ========================================= */
function zfBLMode(mode,button){
document.getElementById("zfBLHtml").style.display = mode === "html" ? "block" : "none";
document.getElementById("zfBLUrl").style.display = mode === "url" ? "block" : "none";
document.querySelectorAll(".zf-blc-tab").forEach(function(tab){ tab.classList.remove("active"); });
button.classList.add("active");
document.getElementById("zfBLMessage").innerHTML = "";
}
/* ========================================= CLEAN HTML ========================================= */
function zfBLCleanHTML(html){
if(!html) return "";
html = html .replace(/</gi,"<") .replace(/>/gi,">") .replace(/"/gi,'"') .replace(/'/gi,"'");
html = html.replace(/```html/gi,""); html = html.replace(/```/g,"");
return html;
}
/* ========================================= ANALYZE HTML ========================================= */
function zfBLAnalyzeHTML(){
var html = document.getElementById("zfBLInput").value;
if(!html.trim()){
zfBLError( "Please paste HTML code first." );
return;
}
html = zfBLCleanHTML(html);
var doc;
try{
var parser = new DOMParser();
doc = parser.parseFromString( html, "text/html" );
}catch(error){
zfBLError( "Unable to analyze this HTML." );
return;
}
var links = doc.querySelectorAll("a[href]");
var results = [];
/* Detect base URL */
var baseTag = doc.querySelector("base[href]");
var baseURL = baseTag ? baseTag.getAttribute("href") : "";
links.forEach(function(link,index){
var href = (link.getAttribute("href") || "").trim();
var text = (link.textContent || "") .replace(/s+/g," ") .trim();
/* Ignore empty links */
if(!href) return;
/* Ignore javascript links */
if( /^javascript:/i.test(href) || /^mailto:/i.test(href) || /^tel:/i.test(href) ){
results.push({
number:results.length + 1, url:href, anchor:text || "No anchor text", type:"Non-web", status:"Skipped", css:"zf-blc-check"
});
return;
}
var absoluteURL = href;
try{
if(baseURL){
absoluteURL = new URL(href,baseURL).href;
}else{
/* Use current site URL for relative links. */
absoluteURL = new URL( href, window.location.href ).href;
}
}catch(error){
results.push({
number:results.length + 1, url:href, anchor:text || "No anchor text", type:"Invalid", status:"Invalid URL", css:"zf-blc-invalid"
});
return;
}
var type = zfBLIsInternal(absoluteURL) ? "Internal" : "External";
/* Hash-only links are navigation anchors, not broken URLs. */
if(/^#/i.test(href)){
status = "Anchor"; css = "zf-blc-check";
}
results.push({
number:results.length + 1,
url:absoluteURL,
anchor: text || "No anchor text",
type:type,
status:"Not checked",
css:"zf-blc-check"
});
});
zfBLDisplay(results);
}
/* ========================================= INTERNAL / EXTERNAL ========================================= */
function zfBLIsInternal(url){
try{
var linkURL = new URL(url);
var current = window.location.hostname;
return ( linkURL.hostname === current || linkURL.hostname.endsWith("." + current) );
}catch(error){
return false;
}
}
/* ========================================= DISPLAY ========================================= */
function zfBLDisplay(results){
document.getElementById("zfBLResults").style.display = "block";
var total = results.length;
var internal = results.filter(function(item){ return item.type === "Internal"; }).length;
var external = results.filter(function(item){ return item.type === "External"; }).length;
var invalid = results.filter(function(item){ return item.type === "Invalid"; }).length;
document.getElementById("zfBLTotal").textContent = total;
document.getElementById("zfBLInternal").textContent = internal;
document.getElementById("zfBLExternal").textContent = external;
document.getElementById("zfBLInvalid").textContent = invalid;
document.getElementById("zfBLSummary").innerHTML = "Analysis Complete: " + total + " links detected. " + internal + " internal, " + external + " external, and " + invalid + " invalid link(s).";
var tbody = document.getElementById("zfBLTable");
tbody.innerHTML = "";
results.forEach(function(item){
var tr = document.createElement("tr");
tr.innerHTML =
"
" +
"
" +
"
" +
"
" +
"
";
tbody.appendChild(tr);
});
document.getElementById("zfBLMessage").innerHTML = "";
}
/* ========================================= URL MODE ========================================= */
async function zfBLAnalyzeURL(){
var url = document.getElementById("zfBLUrlInput").value.trim();
if(!url){
zfBLError( "Please enter a webpage URL." );
return;
}
if(!/^https?:///i.test(url)){
zfBLError( "Please enter a complete URL starting with https:// or http://" );
return;
}
zfBLSuccess( "Fetching webpage HTML..." );
var html = "";
/* First proxy */
try{
var r1 = await fetch( "https://api.allorigins.win/raw?url=" + encodeURIComponent(url) );
if(r1.ok){
html = await r1.text();
}
}catch(error){
html = "";
}
/* Second proxy */
if(!html){
try{
var r2 = await fetch( "https://api.codetabs.com/v1/proxy?quest=" + encodeURIComponent(url) );
if(r2.ok){
html = await r2.text();
}
}catch(error){
html = "";
}
}
if(!html){
zfBLError( "Unable to fetch this webpage. The website may block external requests or require JavaScript. Try Paste HTML mode." );
return;
}
document.getElementById("zfBLInput").value = html;
zfBLAnalyzeHTML();
}
/* ========================================= CLEAR ========================================= */
function zfBLClear(){
document.getElementById("zfBLInput").value = "";
document.getElementById("zfBLUrlInput").value = "";
document.getElementById("zfBLResults").style.display = "none";
document.getElementById("zfBLMessage").innerHTML = "";
document.getElementById("zfBLTable").innerHTML = "";
}
/* ========================================= MESSAGES ========================================= */
function zfBLError(message){
document.getElementById("zfBLMessage").innerHTML = "
";
}
function zfBLSuccess(message){
document.getElementById("zfBLMessage").innerHTML = "
";
}
/* ========================================= ESCAPE ========================================= */
function zfBLEscape(value){
return String(value) .replace(/&/g,"&") .replace(//g,">") .replace(/"/g,""") .replace(/'/g,"'");
}