Your help would be greatly appreciated
I have the following code: It is suppose to get the metatags from a website:
function calculate() {
// This gets the title:
g_title = ('' + document.title + '');
g_title_count = ('' + document.title.split(' ').length + '');
g_title_length = ('' + document.title.length + '')
// This checks all the MetaTags:
g_metadata = document.getElementsByTagName("meta")[i];
g_keywords = "test";
if (typeof g_metadata != 'undefined') {
if (g_metadata) {
var i = 0;
for (i = 0; i <= 10; i++) {
if (g_metadata.name == 'keywords') {
g_keywords[i] = (g_metadata.name[i] + g_metadata.content[i]);
}
}
}
}
}
This value currently returns "test" as specified in the code above:
document.form1.g_keywords.value = g_keywords
However I need it to capture the keywords metatag, description and title.
<title>Test 1</title>
<meta name="keywords"content="test2" />
<meta name="description" content="test3" />
and then write it to a form:
<textarea name="g_keywords" id="g_keywords" cols="80" rows="5" onchange="calculate(this.form1)"></textarea>
It currently works for the Title
but not to get the <meta name="keywords" content="" >
tag
if (g_metadata.name == 'keywords') {
g_keywords[i] = (g_metadata.name[i] + g_metadata.content[i]);
}
I have tried it by creating the above but are not sure how to execute it...
Please help - Thanks
Your help would be greatly appreciated
I have the following code: It is suppose to get the metatags from a website:
function calculate() {
// This gets the title:
g_title = ('' + document.title + '');
g_title_count = ('' + document.title.split(' ').length + '');
g_title_length = ('' + document.title.length + '')
// This checks all the MetaTags:
g_metadata = document.getElementsByTagName("meta")[i];
g_keywords = "test";
if (typeof g_metadata != 'undefined') {
if (g_metadata) {
var i = 0;
for (i = 0; i <= 10; i++) {
if (g_metadata.name == 'keywords') {
g_keywords[i] = (g_metadata.name[i] + g_metadata.content[i]);
}
}
}
}
}
This value currently returns "test" as specified in the code above:
document.form1.g_keywords.value = g_keywords
However I need it to capture the keywords metatag, description and title.
<title>Test 1</title>
<meta name="keywords"content="test2" />
<meta name="description" content="test3" />
and then write it to a form:
<textarea name="g_keywords" id="g_keywords" cols="80" rows="5" onchange="calculate(this.form1)"></textarea>
It currently works for the Title
but not to get the <meta name="keywords" content="" >
tag
if (g_metadata.name == 'keywords') {
g_keywords[i] = (g_metadata.name[i] + g_metadata.content[i]);
}
I have tried it by creating the above but are not sure how to execute it...
Please help - Thanks
Share Improve this question edited Sep 26, 2010 at 16:36 gblazex 50.2k12 gold badges99 silver badges92 bronze badges asked Sep 26, 2010 at 16:16 Gerald FerreiraGerald Ferreira 1,3372 gold badges23 silver badges44 bronze badges2 Answers
Reset to default 6// Collect all keywords
g_metadata = document.getElementsByTagName("meta");
g_keywords = []; // should be an array
var len = g_metadata.length;
for (var i = 0; i < len; i++) {
if (g_metadata[i].name == 'keywords') {
g_keywords = g_metadata[i].content.split(",");
}
}
I think there supposed to be only one keyword <meta>
tag, and all the keywords are listed there separated by ma.
[Demo]
You for
loop is a bit off, you're trying to use i
before it's created, this should be what you're after:
var g_keywords = [], g_description = "No description found",
g_metadata = document.getElementsByTagName("meta")
for (var i=0;i<=g_metadata.length;i++);{
switch(g_metadata[i].name) {
case 'keywords':
g_keywords.push(g_metadata[i].content);
break;
case 'description':
g_description = g_metadata[i].content;
break;
}
}
I'm not sure what you're after with the keywords and metadata, it seems like you want an array for keywords and one string for description, that's what the above does. It loops though the <meta>
tags and if the name
is "keywords" it pushes the .content
onto the g_keywords
array...if it's "description" it sets g_description
to that tag's content
.