简体   繁体   中英

Change meta tag in header dynamically?

I'm creating a shareable connection with a button that the button I clicked before would execute a feature that modifies the OPEN GRAPH after it is posted to social media. I tried to click the button and share it with social media using shared APIs, but the title, link, thumbnail and description did not alter.

const share = $("#change").click(()=>{
   $("meta[name=description]").remove();
   $("head").append("<meta name='description' content='new description'>" 
);})

You can change meta tag using this

 $('button').on('click', function() { $('meta[name=description]').remove(); $('head').append( '<meta name="description" content="this is new">' ); console.log($('meta[name=description]').attr('content')); });
 <head> <meta name="description"content="this is old"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head> <button>Change Meta</button>

You can try this approach too

 $('#mybutton').on('click', function(){ console.log(document.getElementsByTagName('meta')["description"].content); document.getElementsByTagName('meta')["description"].content = "this is new meta tag!"; console.log(document.getElementsByTagName('meta')["description"].content); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <head> <meta name="description"content="this is old meta tag!"> </head> <button id="mybutton">Modify</button>

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