views:

1014

answers:

8

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.

A: 

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.

Ben
A: 

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?

Aaron
+7  A: 

This would be pointless because meta tags are for search engines. Search engines crawlers don't run JavaScript.

Byron Whitlock
Well, you can use meta tags for user-defined purposes. Still probably pointless, since javascript has variables itself.
jholster
maybe for bing and other... for google meta tags are pointless I think
Claudio Redi
+2  A: 

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?

futtta
+1  A: 

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>
Mikael Svenson
+3  A: 

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.

MiffTheFox
I can verify that this works :)
stealthcopter
+4  A: 

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.

Examples

Skype: Switch off phone number parser

<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">

iPhone: Switch off phone number parser

<meta name="format-detection" content="telephone=no">

Google Chrome Frame

<meta http-equiv="X-UA-Compatible" content="chrome=1">

So, it’s not just about search engines.

toscho
I suspect most metas like this have effects that aren't changeable after the page is loaded. But yes, meta is far more than just `keywords` and `description`.
bobince
A: 

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.

egfx