Created
December 6, 2024 09:49
-
-
Save kirilkirkov/0a15a53192dbfd71b74a852702f60d64 to your computer and use it in GitHub Desktop.
H tags semantic order SEO check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
const headings = [...document.querySelectorAll('h1, h2, h3, h4, h5, h6')]; | |
let isOrdered = true; | |
let lastLevel = 0; | |
// Should have at least one H1 | |
const hasH1 = headings.some(heading => heading.tagName === 'H1'); | |
if (!hasH1) { | |
console.warn("Missing H1 heading"); | |
isOrdered = false; | |
} | |
headings.forEach((heading, index) => { | |
const currentLevel = parseInt(heading.tagName[1], 10); | |
// Check if the headings are ordered correctly | |
if (currentLevel > lastLevel + 1 && !(lastLevel === 3 && currentLevel === 2)) { | |
console.warn(`Error in the heading element: ${heading.tagName} at index ${index + 1}`); | |
isOrdered = false; | |
} | |
lastLevel = currentLevel; | |
}); | |
// Check if the headings are ordered correctly | |
if (isOrdered) { | |
console.log("The headings are ordered correctly."); | |
} else { | |
console.warn("The headings are not ordered correctly."); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment