Inject a meta tag from a document's body?

左心房为你撑大大i 提交于 2019-12-10 08:06:41

问题


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?


回答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">');



回答2:


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




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


来源:https://stackoverflow.com/questions/12823059/inject-a-meta-tag-from-a-documents-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!