views:

38

answers:

2

Hi Everyone - 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

+2  A: 

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.

Nick Craver
Thanks for the tips nick!
Gerald Ferreira
+2  A: 
// 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 comma. [Demo]

galambalazs
Hi Galambalazs the Demo did it for me :-) Thank you very much for the help!
Gerald Ferreira
Galambalazs how can I get the g_keywords.length to display the number of characters (if I query g_keywords.length) it gives me the number of words... g_keywords.join(',').lenght = number of characters ?
Gerald Ferreira
`g_keywords.join('').length` without commas, and `g_keywords.join(',').length` with commas
galambalazs