简体   繁体   中英

Can I show / hide meta tags with Javascript

For my site I need to add meta tags for our 3rd party analytics ie

<meta name="WT_si" content="Content Group" />

And basically I want to first figure out the Content Group name based on the url of the page

So in the head tag before I write the meta tag I guess I would like to place some sort of javascript variable in the meta tag for Content group

<meta name="WT_si" content="Content Group" />

Or I was thinking of doing something like

if(contentGroup == "Business)

<meta name="WT_si" content="Content Group" />

else

<meta name="WT_si" content="Corporate" />

Is the above possible in javascript in the head with meta tags >

To add meta tags to the document's head based on a condition with Javascript:

var metaTag=document.createElement("meta");
if(contentGroup == "business")
{
    metaTag.content = "Content Group";
    document.getElementsByTagName('head')[0].appendChild(metaTag);
}
else
{
    metaTag.content = "Corporate";
    document.getElementsByTagName('head')[0].appendChild(metaTag);
}

Something like this should work with jquery

$('meta[content]').hide();

will add style

display:none on the meta tag

but I am not sure why would you want to do that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM