On-Page SEO Analyzer
Analyze your content against key on-page SEO ranking factors.
Content for Analysis
SEO Analysis Report
Overall SEO Score
0
SEO Factor Checklist
Sustainable energy is crucial for our planet's future. This guide explores the various forms of green power. We must transition away from fossil fuels. One key aspect of sustainable energy is solar power, which harnesses the sun's radiation.
Another important area is wind power. Wind turbines are becoming more efficient. Investing in sustainable energy is not just an environmental choice, but an economic one. Learn more on our about page.
`; // --- TAB LOGIC --- let currentTabIndex = 0; const tabs = ['input', 'analysis']; function switchTab(idx) { currentTabIndex = idx; tabBtns.forEach((b, i) => { const tabName = b.dataset.tab; b.classList.toggle('active', tabs[i] === tabs[idx]); }); document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active')); document.getElementById(`${tabs[idx]}-content`).classList.add('active'); } analyzeBtn.addEventListener('click', () => { runAnalysis(); switchTab(1); }); // --- CORE LOGIC --- function runAnalysis() { const htmlString = contentHtmlInput.value; const keyword = targetKeywordInput.value.toLowerCase(); const parser = new DOMParser(); const doc = parser.parseFromString(htmlString, 'text/html'); const checks = {}; let totalScore = 0; // Plain text for density/length checks const textContent = doc.body.textContent || ""; const words = textContent.trim().split(/\s+/).filter(Boolean); const wordCount = words.length; // 1. Title Keyword const title = doc.querySelector('title')?.textContent.toLowerCase() || ""; checks.titleKeyword = { pass: title.includes(keyword) }; if (checks.titleKeyword.pass) totalScore += SEO_FACTORS.titleKeyword.weight; // 2. H1 Keyword const h1 = doc.querySelector('h1')?.textContent.toLowerCase() || ""; checks.h1Keyword = { pass: h1.includes(keyword) }; if (checks.h1Keyword.pass) totalScore += SEO_FACTORS.h1Keyword.weight; // 3. Content Length checks.contentLength = { pass: wordCount > 500, detail: `${wordCount} words` }; if (checks.contentLength.pass) totalScore += SEO_FACTORS.contentLength.weight; // 4. Keyword Density const keywordCount = (textContent.toLowerCase().match(new RegExp(keyword.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g')) || []).length; const density = wordCount > 0 ? (keywordCount / wordCount) * 100 : 0; checks.keywordDensity = { pass: density >= 0.5 && density <= 2.0, detail: `${density.toFixed(2)}%` }; if (checks.keywordDensity.pass) totalScore += SEO_FACTORS.keywordDensity.weight; // 5. Meta Description const meta = doc.querySelector('meta[name="description"]')?.getAttribute('content') || ""; checks.metaDescription = { pass: meta.length > 0, detail: meta.length > 0 ? "Found" : "Missing" }; if (checks.metaDescription.pass) totalScore += SEO_FACTORS.metaDescription.weight; // 6. Internal Links const links = doc.querySelectorAll('a'); checks.internalLinks = { pass: links.length > 0, detail: `${links.length} found` }; if (checks.internalLinks.pass) totalScore += SEO_FACTORS.internalLinks.weight; // 7. Image Alts const images = doc.querySelectorAll('img'); const imagesWithAlts = Array.from(images).filter(img => img.alt && img.alt.trim() !== "").length; checks.imageAlts = { pass: images.length === 0 || imagesWithAlts === images.length, detail: `${imagesWithAlts}/${images.length} with alts` }; if (checks.imageAlts.pass) totalScore += SEO_FACTORS.imageAlts.weight; analysisResults = { score: totalScore, checks, keyword: targetKeywordInput.value, url: contentUrlInput.value }; renderAnalysis(); } // --- UI RENDERING --- function renderAnalysis() { const scoreEl = document.getElementById('seo-score'); const scoreBar = document.getElementById('score-bar'); const checklistContainer = document.getElementById('checklist-container'); scoreEl.textContent = analysisResults.score; scoreBar.style.width = `${analysisResults.score}%`; let scoreColor = 'bg-red-500'; if (analysisResults.score > 75) scoreColor = 'bg-green-500'; else if (analysisResults.score > 40) scoreColor = 'bg-yellow-500'; scoreBar.className = `h-4 rounded-full transition-all duration-500 ${scoreColor}`; checklistContainer.innerHTML = Object.entries(analysisResults.checks).map(([key, result]) => { const icon = result.pass ? `` : ``; return `
${icon}
${SEO_FACTORS[key].label}
${result.detail || (result.pass ? 'Pass' : 'Fail')}
`;
}).join('');
}
// --- PDF GENERATION ---
async function generatePdfReport() {
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = 'Generating...';
const scoreColor = analysisResults.score > 75 ? '#16a34a' : (analysisResults.score > 40 ? '#f59e0b' : '#dc2626');
const checklistHtml = Object.entries(analysisResults.checks).map(([key, result]) => `
${result.pass ? '✔' : '✖'}
${SEO_FACTORS[key].label}
${result.detail || (result.pass ? 'Pass' : 'Fail')}
`).join('');
const reportHtml = `
On-Page SEO Audit
For Keyword: "${analysisResults.keyword}"
${analysisResults.score}
/ 100
${analysisResults.url || 'No URL provided'}
SEO Checklist${new Date().toLocaleDateString()}
${checklistHtml}
Generated by the On-Page SEO Analyzer.
