Add meta tag

M

Martin Honnen

J1C said:
How can I programatically add meta tags with javascript?

The same way you create and add other elements, with the W3C DOM:
var meta;
if (document.createElement &&
(meta = document.createElement('meta'))) {
// set properties
meta.name = "God";
meta.content = "Kibo";

// now add the meta element to the head
document.getElementsByTagName('head').item(0).appendChild(meta);
}

See also
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-37041454>
 
G

Grant Wagner

J1C said:
How can I programatically add meta tags with javascript?

In browsers that support it:

var meta = document.createElement('meta');
meta.name = 'author';
meta.content = 'Your Name';
document.getElementsByTagName('head')[0].appendChild(meta);

However, since the META tag data is mostly for the benefit of search
engines, what does appending a META tag after the page is loaded buy
you? For example, the following doesn't work in IE:

var meta = document.createElement('meta');
meta.setAttribute('http-equiv', 'imagetoolbar');
meta.setAttribute('content', 'no');
document.getElementsByTagName('head')[0].appendChild(meta);

Even though <META HTTP-EQUIV="imagetoolbar" CONTENT="no"> does suppress
the image toolbar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top