Add meta tag

Discussion in 'Javascript' started by J1C, May 16, 2005.

  1. J1C

    J1C Guest

    How can I programatically add meta tags with javascript?
    J1C, May 16, 2005
    #1
    1. Advertising

  2. J1C wrote:

    > 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>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 16, 2005
    #2
    1. Advertising

  3. J1C

    Grant Wagner Guest

    "J1C" <> wrote in message
    news:...
    > 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.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, May 17, 2005
    #3
  4. J1C

    J1C Guest

    great - thanks!
    J1C, May 24, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    758
    John C. Bollinger
    Jan 27, 2006
  2. Nym Pseudo

    META NAME and META HTTP-EQUIV

    Nym Pseudo, Sep 26, 2003, in forum: HTML
    Replies:
    1
    Views:
    531
    =?iso-8859-1?Q?brucie?=
    Sep 26, 2003
  3. Keith Beef
    Replies:
    0
    Views:
    339
    Keith Beef
    Oct 23, 2007
  4. Duane Johnson

    Meta methods to govern meta data?

    Duane Johnson, Oct 25, 2005, in forum: Ruby
    Replies:
    6
    Views:
    223
    Adam Sanderson
    Oct 28, 2005
  5. Erik Veenstra

    Meta-Meta-Programming

    Erik Veenstra, Feb 7, 2006, in forum: Ruby
    Replies:
    29
    Views:
    355
    Erik Veenstra
    Feb 8, 2006
Loading...

Share This Page