Jquery to change meta tag

2019-05-27 06:22发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2019-05-27 06:33

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);
查看更多
Melony?
3楼-- · 2019-05-27 06:45

You just grab a meta tags with the name 'og:url' and set its content attribute like this :

$('meta[property="og:url"]').attr('content', window.location.href);
查看更多
Viruses.
4楼-- · 2019-05-27 06:51

Property not working i used like that:

$("meta[name='og:url']").attr("content", location.href);
查看更多
迷人小祖宗
5楼-- · 2019-05-27 06:53

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

$("meta[property='og:url']").attr("content", location.href);
查看更多
登录 后发表回答