How to extract a website TITLE, META KEYWORDS and META DESCRIPTION by javascript for example site has exactly extracts [www.example.com] websites TITLE, META KEYWORDS and META DESCRIPTION. How to do it by javascript mainly in HTML? Thankyou!

Dani AI

Generated

and are right: JavaScript can read only the page it is running on. If you need the title/description/keywords for another site (like asked), browsers will block direct cross-site reads. The practical pattern is: call your server with the target URL, have the server fetch and parse the HTML, then return JSON to your page. Also, avoid regex for HTML; use a real DOM parser.

Here is a minimal, robust PHP example using DOMXPath. It handles redirects, mixed-case meta names, and falls back to Open Graph/Twitter description when no meta description exists.

function fetchMeta($url) {
    $ctx = stream_context_create([
        'http' => ['user_agent' => 'Mozilla/5.0', 'timeout' => 6, 'follow_location' => 1]
    ]);
    $html = @file_get_contents($url, false, $ctx);
    if ($html === false) return null;

    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xp = new DOMXPath($doc);

    $title = trim($xp->evaluate('string(//title)'));
    $desc  = trim($xp->evaluate('string(//meta[translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="description"]/@content)'));
    if ($desc === '') {
        $desc = trim($xp->evaluate('string(//meta[@property="og:description"]/@content | //meta[@name="twitter:description"]/@content)'));
    }
    $keys  = trim($xp->evaluate('string(//meta[translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="keywords"]/@content)'));
    return ['title' => $title, 'description' => $desc, 'keywords' => $keys];
}

Typical flow:

  • Client sends POST /extract with { url }.
  • Server runs fetchMeta($url) and returns JSON.
  • Client renders the values.

Tips: many pages do not have meta keywords; do not rely on them. Some pages build content with JavaScript; a simple HTTP fetch may miss those, so a headless fetch (e.g., server-side browser) may be needed. And thanks for keeping the discussion in one place so answers do not fragment.

Recommended Answers

All 10 Replies

Well, assuming you really want JS, you can just do document.getElementsByTagName('title') and ...('meta'). If you actually get this info on another server, without running any code from that page, what I would find more logical, you could use an XML parser or just some regular expression to get the info.

actually i am new to coding!
so if you give me a example or refer any article that will better for me.
Thankyou

Thanks twiss,
suppose if i want to extract www.google.com "Title" then what changes needed for below code?

function getTitle($Url){
	$str = file_get_contents($Url);
	if(strlen($str)>0){
		preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
		return $title[1];
	}
}
//Example:
echo getTitle("http://localhost/");
echo getTitle('http://google.com/');

Hello,
here i am showing you a code that extract a page TITLE, DESCRIPTION perfectly.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>amar titkle</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="description" content="shob kisur bornona" />
<meta name="keywords" content="eta ki, googl , bd, in" />
<style type="text/css">
#Box{border:1px solid #000044;text-align:center;background-color : #fff;} 
#green {color:green }
</style>
</head>
<body><h1>Niche Dekhun</h1><br />
<div id="Box">
<script type="text/javascript">
//<![CDATA[
var a = (document.title);
var b = (document.location.href);
var c = document.getElementsByTagName('meta');
var description;
for (var x = 0, y = c.length; x < y; x++) {
    if (c[x].name.toLowerCase() == "description") {
        description = c[x];
    }
}
document.write('<a href="' + b + '">' + a + '<\/a><br />' + description.content + '<br />');
document.write('<span id="green">' + b + '<\/span>');
//]]>
</script>
</div>
</body>
</html>

What then if i want to extract TITLE, DESCRIPTION of a URL by this above code in my website? What types of changes i need to do in this code?
Hoping a great response!
Thankyou

Merging this with your other thread for the same question.

Please do not scatter the discussion over multiple threads.

this is a new code i find over net, so it can'?

It is still basically the same question. Spreading that across many threads just creates a cluttered mess and people may waste their time typing duplicate suggestions that have already been given in another thread.

only javascript can do that document.title and document.getElementsByTagName('meta') will do the job .. load this code into the 2 var needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.