Pao*_*ino 52
gravatar url看起来像这样:
http://www.gravatar.com/avatar/<md5hashofemail>
Run Code Online (Sandbox Code Playgroud)
所以你要做的就是包含一个名为md5的函数,它返回用户电子邮件的md5哈希值.网上有很多人这样做,但我相信https://github.com/blueimp/JavaScript-MD5/blob/master/README.md效果很好.之后,就这样做:
// get the email
var email = $('#email').val();
// -- maybe validate the email?
// create a new image with the src pointing to the user's gravatar
var gravatar = $('<img>').attr({src: 'http://www.gravatar.com/avatar/' + md5(email)});
// append this new image to some div, or whatever
$('#my_whatever').append(gravatar);
// OR, simply modify the source of an image already on the page
$('#image').attr('src', 'http://www.gravatar.com/avatar/' + md5(email));
Run Code Online (Sandbox Code Playgroud)
我认为这很明显,但我会为了后人的缘故加上它:
如果用户电子邮件是私有的,并且您在列表中显示此ala-stackoverflow,则最好在服务器上对电子邮件进行编码,以便在查看源时,用户电子邮件不会公开显示.