Inject a meta tag from a document's body?

19,779

Solution 1

Maybe you can try this:

jQuery:

$('head').append('<meta name="apple-itunes-app" content="app-id=xxxxx">');

Javascript:

document.getElementsByTagName('head')[0].appendChild('<meta name="apple-itunes-app" content="app-id=xxxxx">');

Solution 2

You can add the meta tag but as they're used before the body is even parsed, it's useless.

Solution 3

$('head').append(""); << doesn't work, I even saw this in document.ready, like dystroy says: you need the tag before the body is even parsed....

For me, this did the job:

  1. Put this in the head section, it will detect and write the meta before the body is parsed. If you don't have any conditions, just paste the html in the head section:
<script type="text/javascript">
     var isiPad = navigator.userAgent.match(/iPad/i) != null;
        if(isiPad === false){
          document.write('<meta name="apple-itunes-app" content="app-id={xxxxx}">');
        }
</script>
Share:
19,779
Steve
Author by

Steve

Updated on June 14, 2022

Comments

  • Steve
    Steve almost 2 years

    I need to add a meta tag (specfically, <meta name="apple-itunes-app" content="app-id=xxxxx">) to a certain page, but the way our templates are set up, it's not possible for me to edit the code for the HEAD tag directly (for corporate, not technical, reasons).

    Therefore, is there a way using JQuery within the BODY tag to add this meta tag?

  • Steve
    Steve over 11 years
    I was afraid of that. The above method does write the tag, but as you said it does not work as it should.
  • Steve
    Steve over 11 years
    Well, technically those would be the correct ways to inject a tag into the head so hopefully if someone wants to do that they'll find the answer. I voted up your answer since there's no way to mark more than one as "right".
  • benathon
    benathon about 11 years
    The jQuery version worked great for me. In my application I do control the head, however I wanted to make this banner dynamic in javaScript. I put the jQuery line here in a script tag as the last thing before </head> and iOS detects it correctly.
  • Johannes Fahrenkrug
    Johannes Fahrenkrug over 8 years
    That's not entirely true. Certain meta tags work even when they are added after the body has been parsed, see stackoverflow.com/questions/3588628/…