2012-03-03 94 views
1

我试图动态设置使用javascript共享到Facebook时显示的缩略图。我试图添加元标记“og:image”到页面(这是一个JSP),并且工作,但我现在要做的是用javascript动态加载另一个图像。使用javascript设置Facebook共享器显示的图像

基本上,页面在加载时使用javascript调用API,并检索图像列表。我想用其中的一个作为缩略图。

我尝试使用javascript来替换元标记的内容,但Facebook似乎并不在乎它(它会改变,如果我检查我的浏览器)。

可以做到这一点吗?

在此先感谢!

回答

0

Facebook机器人从不运行java脚本代码 但为什么你不尝试在服务器端设置og标签?

+0

我看了你可以使用#!使它运行的JavaScript,但不知道我确切地了解是否以及如何应该工作。我想避免运行服务器端代码,这意味着需要更改现有代码。也许可以使用 – 2012-03-03 11:02:16

+0

,但javascript并不是完全可信的解决问题的方法 – 2012-03-03 11:08:13

1

这里是我用来提取从闪存对象标签的flashvars参数之后,图像的URL,然后通过使用jQuery分配给meta标签的功能:

$(window).load(function(){ 
//Use $(window).load() instead of $(document).ready(), so that the flash code has loaded and you have all the html you need process with javascript already in place when you start processing. 
    var stringToExtractFrom = $('param[name="flashvars"]').attr('value'); 
//Get the flashvars parameter value which we'll use to extract the preview image url from. 
    var pos = stringToExtractFrom.indexOf("&"); 
//Search for the position ampersand symbols which surround the image url. 
    var stringToUse; 
//The final string we'll use. 
    var startOfImageSrc = null; 
//The first position where we discover the ampersand 
    var endOfImageSrc; 
//The second position where we discover the ampersand 
    var lengthToSubstract 
//How many symbols to chop off the flashvars value. 
    while(pos > -1) { 
     if(startOfImageSrc == null){ 
      startOfImageSrc = pos; 
     } 
     else { 
      endOfImageSrc = pos; 
      lengthToSubstract = endOfImageSrc - startOfImageSrc; 
     } 
     pos = stringToExtractFrom.indexOf("&", pos+1); 
    } 
    stringToUse = stringToExtractFrom.substr(startOfImageSrc+7, lengthToSubstract-7); 
    $('meta[property="og:image"]').attr('content', stringToUse); });