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

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/");

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'?@Ezzaral

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.