JavaScript Code to Get Page Keywords

CK CK 3788 
Created at  
353 0 0

Keywords are important because they help convey the subject and purpose of content to both users and search engines. In the context of web development and SEO (Search Engine Optimization), keywords serve several critical roles, including improving discoverability, relevance, and targeting. However, their importance has evolved over time, especially with the changes in how search engines rank content.

To extract the keywords from an HTML page using JavaScript, you need to target the <meta name="keywords"> tag in the document. Here's how to retrieve the value of the content attribute of this meta tag:

JavaScript Code to Get Page Keywords:

function getPageKeywordsFromHtml(html) {
  // Create a DOM parser to parse the HTML string
  const parser = new DOMParser();
  const doc = parser.parseFromString(html, 'text/html');

  // Look for the meta tag with name="keywords"
  const keywordsMeta = doc.querySelector('meta[name="keywords"]');

  // Return the content attribute of the meta tag, or null if not found
  return keywordsMeta ? keywordsMeta.getAttribute('content') : null;
}

// Example HTML input (replace with your HTML content)
const htmlText = `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="keywords" content="HTML, JavaScript, CSS, Web Development">
  <title>Example Page</title>
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>
`;

// Extract the page keywords
const pageKeywords = getPageKeywordsFromHtml(htmlText);
console.log(pageKeywords); // Outputs: "HTML, JavaScript, CSS, Web Development"

Explanation:

  1. DOMParser: Converts the HTML string into a DOM object.
  2. meta[name="keywords"]: Searches for the meta tag with the name="keywords" attribute.
  3. getAttribute('content'): Retrieves the content of the keywords meta tag, which typically contains a comma-separated list of keywords relevant to the page.

Output:

If the meta keywords tag is found, the function returns the keywords as a string. If not, it returns null.

Example Use Case:

If the page has a meta keywords tag like this:

html

코드 복사

<meta name="keywords" content="HTML, JavaScript, CSS, Web Development">

The output will be:

arduino

코드 복사

"HTML, JavaScript, CSS, Web Development"

Tags Meta Tags Site Metadata Web Page Keywords Facebook X
Comments 0
Similar posts
  1. What is OpenGraph?
    367
  2. What is Twitter Cards?
    405
  3. Code to Extract the Site Thumbnail Image for Twitter or Facebook
    395
  1. Tailwind CSS and shadcn/ui for React App with Charts and Graphs
    1,049
  2. HTML NoIndex: Keep Pages Off Search
    1,499
  3. JavaScript to disable Screen Saver or Power Saving Mode
    509
  4. Improving SEO with image-sitemap
    1,183
  5. Making HTML Tables Responsive on Mobile: Addressing Text Overflow
    783
  6. How do we make better SEO with schema.org?
    428
  7. A Javascript code that returns Monthly Mortgage Payment and Tax
    1,266
  8. A simple JavaScript snippet to copy a string to the clipboard
    1,058
  9. Dynamic JSON Editor based in HTML/JavaScript
    514
  10. How to post content (image or video) to Instagram using the Instagram Graph API
    6,412
  11. Font name correctly having the same width space
    1,229
  12. JavaScript Algorithms
    722
  13. GraphQL Tutorial on CentOS 7: From Installation to Deployment
    564
  14. Capture all browser console logs including errors for debugging
    2,192
  15. Simple asynchronous/synchronous $.ajax() example using jQuery to make an HTTP GET request
    862
  16. JavaScript Code to Get the Page Title
    340
  17. JavaScript Code to Get Meta Description from HTML
    398
  18. Javascript code to extract favicon URL
    404
  19. RSS to Array in JavaScript
    2,168
  20. Javascript function to convert sitemap.xml to array
    602
  21. Understanding Elasticsearch from Installation to Trouble Shooting
    915
  22. IndexNow for better SEO
    320
  23. Enhancing SEO for Single Page Applications: Implementing Dynamic URL Updates
    1,043
  24. Method to ignore JavaScript Errors globally instead of try... catch for each of cases
    973
  25. Method to catch the browser console log and errors for remote web debugging
    2,191
  26. Atlassian Jira Ticket API Response Example
    2,807
  27. Three different type of Word-Wrap options in HTML CSS
    1,189
  28. JavaScript code to load XML Document
    604
  29. Creating a Pinterest-Style Card Layout with Bootstrap and Masonry
    599
  30. Equal Height Blocks in Bootstrap with JavaScript
    713
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,185
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Array of arguments passed to script in PHP
    98
  9. Invalid Auction Storms in Ad Tech
    107
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    208
  11. The Two Clocks of Ad Server Architecture
    78
  12. Frequency Cap Architecture Overview
    527
  13. Collecting Proof-of-Play from 100,000 Screens
    153
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  15. What is RedisVL?
    116
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  17. TPU & GPU Cluster Management Tools
    258
  18. The Concept of Scaffolding in AI Agents
    245
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    268
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    354
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    203
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233