if I put a div in the head and display:none, than use javascript to display it, will this work?
Edit:
I have stuff loaded in ajax. And as my ajax changes the "main" portion of the site, I want to change the meta-tags as well.
if I put a div in the head and display:none, than use javascript to display it, will this work?
Edit:
I have stuff loaded in ajax. And as my ajax changes the "main" portion of the site, I want to change the meta-tags as well.
No, a div is a body element, not a head element
EDIT: Then the only thing SEs are going to get is the base HTML, not the ajax modified one.
divs only display in the body. Curious, though: what is your reason for wanting to put a div in the head? And what is your reason for wanting to change meta tags?
This would be pointless because meta tags are for search engines. Search engines crawlers don't run JavaScript.
meta-tags are part of the dom and can be accessed and -i guess- changed, but search-engines (the main consumers of meta-tags) won't see the change as the javascript won't be executed. so unless you're changing a meta-tag (refresh comes to mind) which has implications in the browser, this might be of little use?
It should be possible like this (or use jQuery like $('meta[name=author]').attr("content");):
<html>
<head>
<title>Meta Data</title>
<meta name="Author" content="Martin Webb">
<meta name="Author" content="A.N. Other">
<meta name="Description" content="A sample html file for extracting meta data">
<meta name="Keywords" content="JavaScript, DOM, W3C">
</head>
<body>
<script language="JavaScript"><!--
if (document.getElementsByName) {
var metaArray = document.getElementsByName('Author');
for (var i=0; i<metaArray.length; i++) {
document.write(metaArray[i].content + '<br>');
}
var metaArray = document.getElementsByName('Description');
for (var i=0; i<metaArray.length; i++) {
document.write(metaArray[i].content + '<br>');
}
var metaArray = document.getElementsByName('Keywords');
for (var i=0; i<metaArray.length; i++) {
document.write(metaArray[i].content + '<br>');
}
}
//--></script>
</body>
</html>
You'd use something like (with jQuery):
$('meta[name=author]').attr('content', 'New Author Name');
But that would be mostly pointless as meta tags are usually only scraped when the document is loaded, usually without executing any JavaScript.
This is just a comment, but I need line breaks.
There are real use cases: Some browsers and plugins parse meta elements and change their behavior for different values.
<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
So, it’s not just about search engines.
Yes, it is possible to add metatags with Javascript. I did in my example
http://stackoverflow.com/questions/3604886/android-not-respecting-metatag-removal
But, I dont know how to change it other then removing it. Btw, in my example.. when you click the 'ADD' button it adds the tag and the viewport changes respectively but I dont know how to revert it back (remove it, in Android).. I wish there was firebug for Android so I saw what was happening. Firefox does remove the tag. if anybody has any ideas on this please note so in my question.