使用 JQuery 更改 Facebook 元标记内容属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7054217/
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
Changing Facebook meta tag Content Attribute using JQuery
提问by Idan Shechter
I want to know how can I use JQuery in order to replace the content attribute of my Facebook meta tags.
我想知道如何使用 JQuery 来替换我的 Facebook 元标记的内容属性。
I searched StackOverflow and what I've found was this example:
我搜索了 StackOverflow,我发现的是这个例子:
$("meta[property=og:title]").attr("content", title);
$("meta[property=og:url]").attr("content", url);
In my code I have the following meta tags:
在我的代码中,我有以下元标记:
<meta property="og:title" content="some content"/>
<meta property="og:url" content="some content"/>
<meta property="og:site_name" content="some content"/>
When I run the function that should change the content for the 'title' and 'url' variables, the change doesn't take place. The same values for the 'content' still remains the same.
当我运行应该更改“title”和“url”变量内容的函数时,更改不会发生。“内容”的相同值仍然保持不变。
I want to know how, if it's possible, to use JQuery to change the value of Facebook's meta tags.
我想知道如果可能的话,如何使用 JQuery 来更改 Facebook 元标记的值。
回答by Jinn
This is how it worked for me:
这对我来说是这样的:
$('meta[name=og\:url]').attr('content', newVideoUrl);
note the double backslash, without it Jquery will fail with an error.
注意双反斜杠,没有它Jquery 将失败并出现错误。
回答by Jeff Sherlock
It is definitely possible to change meta tags using jQuery, but I don't think that's going to accomplish what you want since the meta tags are pulled by the FB scraper. The FB scraper goes out and scrapes your page to read the meta tags.
绝对可以使用 jQuery 更改元标记,但我认为这不会实现您想要的,因为元标记是由 FB 刮板拉取的。FB 抓取器会启动并抓取您的页面以读取元标记。
Editing Meta Tags
You can update the attributes of your page by updating your page's tags. Note that og:title and og:type are only editable initially - after your page receives 50 likes the title becomes fixed, and after your page receives 10,000 likes the type becomes fixed. These properties are fixed to avoid surprising users who have liked the page already. Changing the title or type tags after these limits are reached does nothing, your page retains the original title and type.
For the changes to be reflected on Facebook, you must force your page to be scraped. The page is scraped when an admin for the page clicks the Like button or when the URL is entered into the Facebook URL Linter. You can programmatically force your page to be scraped by cURL'ing the linter. For example ...
编辑元标签
您可以通过更新页面的标签来更新页面的属性。请注意 og:title 和 og:type 最初只能编辑 - 在您的页面收到 50 个赞后,标题变为固定,在您的页面收到 10,000 个赞后,类型变为固定。这些属性是固定的,以避免让已经喜欢该页面的用户感到惊讶。达到这些限制后更改标题或类型标签没有任何作用,您的页面保留原始标题和类型。
要使更改反映在 Facebook 上,您必须强制抓取您的页面。当页面管理员单击“赞”按钮或将 URL 输入到 Facebook URL Linter 时,页面会被抓取。您可以通过 cURL'ing linter 以编程方式强制抓取您的页面。例如 ...
回答by elMarquis
The Facebook scraper doesn'texecute JavaScript.
Facebook 抓取工具不执行 JavaScript。
So using Javascript to modify the metatags in order to show different share content won't work.
因此,使用 Javascript 修改元标签以显示不同的共享内容是行不通的。
You'd need to use serverside code to write them out as needed in the HTML source.
您需要使用服务器端代码在 HTML 源代码中根据需要将它们写出来。