使用jQuery加载Gravatar [英] Loading gravatar using jquery
问题描述
只需尝试在博客上创建一个简单的评论表单即可.我想在用户将其写在电子邮件框中时加载用户的图片(使用jQuery).
Just trying to crate a simple comment form on a blog. I want to load the user's gravatar (using jQuery) when he/she writes this in the email box.
我该怎么做?
推荐答案
引人入胜的网址看起来像这样:
The gravatar url looks like this:
http://www.gravatar.com/avatar/<md5hashofemail>
因此,您要做的就是包含一个名为md5的函数,该函数返回用户电子邮件的md5哈希.有很多在线工具可以做到这一点,但是我相信 https://github.com. com/blueimp/JavaScript-MD5/blob/master/README.md 效果很好.之后,只需执行以下操作即可:
So all you're going to have to do is include a function called md5 that returns the md5 hash of the user's email. There are many online that do this, but I believe https://github.com/blueimp/JavaScript-MD5/blob/master/README.md works well. After that, just do:
// 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));
我认为这很明显,但是为了后代,我将其添加:
I thought this was obvious, but I will add it for posterity's sake:
如果用户电子邮件是私有的,并且您在列表中显示了ala-stackoverflow,则最好在服务器上对电子邮件进行编码,这样,如果您查看源,则不会公开显示用户电子邮件.
If user emails are private and you're showing this ala-stackoverflow in a listing, you are probably better off encoding the email on the server so that user emails are not publicly visible if you look at the source.
这篇关于使用jQuery加载Gravatar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!