Using JQuery to add metadata in the Head Tag

2020-07-05 09:26发布

I'm trying to insert a new meta tag within the Head, I'm using a content managment system that won't allow editing within the Head so I'm attempting to do this using jQuery. Unfortunately I can't it to work.

This is the code I've added to the following webpage: http://www.newcastlegateshead.com

<script type="text/javascript"> 
$("head").append("<meta name=viewport content=width=400, initial-scale=0.45, minimum-    scale=0.45/><link rel=apple-touch-icon href=/images/customIcon.png/><meta name=apple-mobile-web-app-capable content=no /><meta name=apple-mobile-web-app-status-bar-style content=black-translucent /><link rel=apple-touch-icon-precomposed href=/images/customIcon.png/> ");
</script>

标签: jquery
2条回答
戒情不戒烟
2楼-- · 2020-07-05 09:54

This worked for me ... thank you @thecodeparadox. For me to get it to work I needed single quotes not double quotes before and after

<script type="text/javascript"> 
     $(document).ready(function() {
         $("head").append('<meta name="apple-mobile-web-app-capable" content="yes">');
     });
</script>
查看更多
甜甜的少女心
3楼-- · 2020-07-05 10:02

Try your code within

<script type="text/javascript"> 
     $(document).ready(function() {
         $("head").append("<meta name=viewport content=width=400, initial-scale=0.45, minimum-    scale=0.45/><link rel=apple-touch-icon href=/images/customIcon.png/><meta name=apple-mobile-web-app-capable content=no /><meta name=apple-mobile-web-app-status-bar-style content=black-translucent /><link rel=apple-touch-icon-precomposed href=/images/customIcon.png/> ");
    });
</script>

Short form of $(document).ready(function() {...}) is:

jQuery(function ($) {
    // Your code
})

This will ensure that you code will execute during onload.

查看更多
登录 后发表回答