通过 JavaScript 更新 Facebook Open Graph 元标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3081004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Facebook Open Graph meta tag through JavaScript
提问by simmu
I'm trying to update the meta content of the Facebook's Open Graph title. I updated the content, but when it published the feed, it's still pulling the old information.
我正在尝试更新 Facebook 的 Open Graph 标题的元内容。我更新了内容,但是当它发布提要时,它仍然在拉旧信息。
Example:
例子:
<meta property="og:title" content="Title 1"/>
I managed to update to
<meta property="og:title" content="Title 2"/>
When I click on the Facebook Like, it's still pointing the title to "title 1".
当我点击 Facebook Like 时,它仍然将标题指向“title 1”。
How can I make it update?
我怎样才能让它更新?
回答by Hans Skov
I'm guessing you figured this out already, but others may have the same problem.
我猜你已经想通了,但其他人可能也有同样的问题。
You can't change the Open Graph meta tags with JavaScript after the pageload, as Facebook requests the page from the server again when looking for the meta tags, and so it will not find the updated tags.
您不能在页面加载后使用 JavaScript 更改 Open Graph 元标签,因为 Facebook 在查找元标签时会再次从服务器请求页面,因此它不会找到更新的标签。
Furthermore, I found that Facebook only searches the header for the Open Graph meta tags, so adding them in the body will not work either.
此外,我发现 Facebook 只搜索 Open Graph 元标记的标题,因此将它们添加到正文中也不起作用。
回答by Noah King
Facebook scrapes pages for meta-data only when it is necessary. After you make changes to the metadata, you need to resubmit the object with the 'scrape' parameter set to 'true' in your POST request.
Facebook 仅在必要时为元数据抓取页面。对元数据进行更改后,您需要在 POST 请求中重新提交将 'scrape' 参数设置为 'true' 的对象。
This is outlined in the Open Graphdocumentation for updating objects.
这在用于更新对象的Open Graph文档中进行了概述。
回答by Anand Agrawal
The meta tags are read by Facebook when shared (liked, etc.), so the JavaScript code might not work. You can test using the online debugger.
元标记在分享(喜欢等)时由 Facebook 读取,因此 JavaScript 代码可能不起作用。您可以使用在线调试器进行测试。

