简体   繁体   中英

Jquery to change meta tag

I am trying to make this meta tag always have the current URL of the page it is on.

<meta property="og:url" content="http://shop.famsf.org/do/product/BK5160" />

How would I go about changing a meta tag with JS or JQ?

Changing Facebook meta tag Content Attribute using JQuery

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.

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 ...

https://developers.facebook.com/docs/opengraph/

BUT if you want to do this:

$('meta[property=og\\:url]').attr('content', window.location.href);

Select the element using tagname and attribute. Set the value using .attr() :

$("meta[property='og:url']").attr("content", location.href);

您只需获取名称为'og:url'的元标记,并按如下所示设置其content属性:

$('meta[property="og:url"]').attr('content', window.location.href);

我无法使用像我这样的财产:

$("meta[name='og:url']").attr("content", location.href);

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