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

4

7

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"

Tenacious answered 14/6, 2017 at 19:15 Comment(0)
C
6

Change it with jQuery like:

$('meta[property=og\\:image]').attr('content', 'http://myweb.com/image.jpg');
Creditor answered 2/2, 2018 at 11:40 Comment(0)
F
4

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

Forenoon answered 14/6, 2017 at 19:41 Comment(0)
D
1

for javascript:

document.querySelectorAll('meta[property=og\\:image]')[0].setAttribute('content', 'http://myweb.com/image.jpg')
Disfigurement answered 3/5, 2022 at 2:5 Comment(0)
I
0

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

Idalia answered 14/6, 2017 at 19:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.