用 jquery 影响元标签或链接标签?
首先是一个简短的解释...以下两行确保一旦我的网站在 Facebook 上共享,就会发布自定义图像 (share-thumbnail.png)。
<meta property="og:image" content="images/share-thumbnail.png"/>
<link rel="image_src" href="<images/share-thumbnail.png" />
当我点击页面上的特定共享链接时,你们能想到任何有创意的方法来覆盖该行为吗?
例如,我的页面上有一个 与特殊图像共享
锚点,当单击该锚点时,我没有想要使用我的标头中定义的 share-thumbnail.png 。
我的想法是使用 jquery 监听 .special-sharing 链接上的点击事件,然后… preventDefault();并将头部中元标记的内容属性更改为新图像 src。然后调用 facebook?sharer=blabla。
你会如何解决这个问题?
first off a short explanation... the following two lines assure that once my website is shared on facebook a custom image (share-thumbnail.png) is posted.
<meta property="og:image" content="images/share-thumbnail.png"/>
<link rel="image_src" href="<images/share-thumbnail.png" />
Can you guys think of any creative way to overwrite that behaviour when I click a specific sharing link on my page?
E.g. i have a <a class="special-sharing" href="#">Share with special image</a>
anchor on my page and when that one is clicked I don't want to use the share-thumbnail.png defined in my header.
My thought was to use jquery to listen to click events on the .special-sharing link and then… preventDefault(); and change e.g. the content attr of my meta tag in the head to a new image src. Then call the facebook?sharer= blabla.
How would you solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(警告:我不熟悉 Facebook 的 API,只熟悉 Javascript 和一般的网络。)
不幸的是,你不能。
虽然可以使用 jQuery(或一般的 Javascript)影响
和
标记,但这是不可能的在用户浏览器中当前加载的页面副本之外进行更改。当调用 Facebook 的链接时,当前页面将被丢弃并加载 Facebook 的页面,然后在您的页面上执行其自己点击以获取
og:image
,该图像仍将具有原始值,因为服务器上的文件没有更改。相反,您也许可以设置服务器,以便可以通过 URL 自定义图像。例如,让
http://www.example.com/mypage.php
为og:image
生成默认值,但http://www. example.com/mypage.php?special-image=foo.gif
更改标记的值。然后,您可以使用 Javascript 更改传递给 Facebook 的共享者,以便它使用指向特殊图像的特殊 URL。
(Caveat: I'm not familiar with Facebook's API, just Javascript and the web in general.)
Unfortunately, you can't.
While it's possible to affect
<meta>
and<link>
tags with jQuery (or Javascript in general), it isn't possible to make changes outside the currently-loaded copy of the page in the user's browser. When the link to Facebook is called, the current page is discarded and Facebook's page is loaded, which then performs its own hit on your page to get theog:image
, which will still have the original value because the file on the server hasn't changed.Instead, you could perhaps set up your server so that you can customize the image via the URL. For example, have
http://www.example.com/mypage.php
produce the default value forog:image
, buthttp://www.example.com/mypage.php?special-image=foo.gif
change the value of the<meta>
tag. You could then use Javascript to change the sharer passed to Facebook so that it uses the special URL that points to the special image.