简体   繁体   中英

Dynamically adding meta tags using javascript or jquery [twitter cards]

I want to add twitter card meta tags to my website. i cannot add static tags since the content attribute in the meta tag has to change dynamically. Plz help if someone has a solution.

You can't, at least not in any way that Twitter is going to recognise.

When Twitter fetches the page, it is always going to get the meta elements that are in the HTML, never ones added with client side JavaScript.

If you are changing content dynamically then make use of the history API to update the URI, and make sure that the server will generate all the content for each URI when the URI is used as an entry point (you can still use JavaScript to generate the content when moving from another page on the site).

You can generate pages dynamically (server side) as long as you don't intend to change them after. Think about the process in the same way you would build landing pages for SEO purposes. Twitterbot is in many ways really similar to Googlebot and similar indexers.

See https://dev.twitter.com/blog/twitter-cards-tips-tricks for a few examples of sites doing this.

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