简体   繁体   中英

Dynamically changing the content of: meta property="og:image"

I want to change the main photo in a web page, I just have the url page. So I decided to use the meta written for Facebook sharing.

I want to change the image in: meta property="og:image" content="http://myweb.com/image.jpg"

使用 jQuery 更改它,例如:

$('meta[property=og\\:image]').attr('content', 'http://myweb.com/image.jpg');

You can change og:image with following code:

$('meta[name=og\\:image]').attr('content', newVideoUrl);

But, if you want to change the image permanently (so Facebook can scrape your data and the image will be available for sharing), you need to change this value on the server.

Facebook is reading <meta og:image> only from the response of the server.

See similar topic: Facebook scraper doesn't load dynamic meta-tags

for javascript:

document.querySelectorAll('meta[property=og\\:image]')[0].setAttribute('content', 'http://myweb.com/image.jpg')

I think this can be useful to you. Instead of getAttribute you should use setAttribute and thats all. :-)

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