September 17, 2003

Reading META tags with Javascript

I was running into some problems trying to reference some META tags with Javascript. It was three simple roadblocks. tags/getElementsByTagName, .name and .content. I felt stupid by the time I got this working. Bottom line, I was thinking that META tags were somehow different and special from the DOM. Don't know why, just was particularly stupid this morning. They are, so you can find them in your standard DOM methods. Then I just had to reference their .name to find the META tag I wanted, and then the .content to find the value. As Anne pointed out in the comments, this javascript isn't ideal nor perfect. So it still needs work. I just haven't done that work yet. But much thanks to Anne for steering me in the correct direction.
Post Info

Tagged As Coding, Javascript

Comments are Open (6)

Posted at 10:38 AM

Comments

Anne

You understand that this method is not DOM-compliant I hope. 'document.write' is out of date and so is document.all, which is completely unnecessary for this.

You should use document.createElement and appendChild and so on.

Tony

Dammit. I'll update that. Thanks Anne. I was mostly just force-coding from memory. Didn't even think about compliance. I do believe I'll need the document.all for older IE's. We have a pretty old baseline of browsers that I'm doing this for. Once I get my test platform, I'll try it without. (or just email you directly.)

This is twice now you've given me very clear and correct advice. (or pointed out my error). Keep on doing this and I'll start to develop an inferiority complex. ;)

liorean

Remember that the author might use either http-equiv or name. It might be an idea to check both.

Tony

True. I'll probably put that in to be sure. But I do know exactly how they will put it in, as it is part of the standard set forth. But having it check both is the right thing to do.

Anne

http-equiv is only a equivalent of the server settings if anything is correct. It should actually not be used at all (the http-equiv attribute).

Richard

I tried the code however I recieved the following error [MetaTagList.length: is null or not an object ]

Can you help me ?
Have you updated the code ?

Thanks

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Search
Comments on this post
Categories
Info
Tony Stephens
Copyright © 1995-2005
Site Version:
10
Licensed:
Creative Commons
Validate:
XHTML, CSS, 508, RSS
Subscribe
Blog-Fu, Link-Fu