Reputation: 439
i have some code which takes in a html using ajax and after which it's meta tags are retrieved.
if (request.readyState == 4) {
var html_text = request.responseText;
var parent = document.createElement('div');
parent.innerHTML = html_text;
var metas = parent.getElementsByTagName('meta');
var meta;
for(var i=0; i < metas.length; i++) {
meta = metas[i];
alert(meta.property);
alert(meta.content); }
}
the html_text does contains meta property and content and the content does show. but why is the meta property showing as undefined? can anyone help me with this?
Upvotes: 0
Views: 1651
Reputation: 117334
What you try here is a kind of creation of a new document, it will not work at least in IE this way.
Put this line
alert(parent.innerHTML)
right after:
parent.innerHTML = html_text;
...and you will see, that you only get the contents of the body, everything else has been omitted.
If the response is valid xml, request.responseXML
should be available, you can inspect it directly(it's already a document).
Upvotes: 0
Reputation: 33904
Either you have to look for meta.name
or you could use meta.getAttribute("property")
.
btw: You are innerHTML'ing the variable html_code
but you stored the HTML content in html_text
.
Upvotes: 1
Reputation: 987
You could try using getAttribute to get the property attribute:
alert(meta.getAttribute('property'));
I'm not sure why it wouldn't work your way though.
Upvotes: 0