ItSolutionStuff.com

How to Get Gravatar Image from Email using JQuery?

By Hardik Savani • June 17, 2023
jQuery

Sometimes, we require to get gravatar image from email address in jquery. So, today i am going to share with you how to get gravatar image from email.

Gravatar is a service for providing globally unique avatars. We can simply create our account and set avatar image that we want. There are several developer and users are using this service. So, that way any of site we can simply get avatar image from email address from Gravatar account.

Today, in this example i also checked everything, If email does not registered then we can Set default image if user doesn't have Gravatar. So let's follow bellow example.

In this example i include following js and css.

1)bootstrap.min.css

2)jquery.min.js

3)jquery.md5.js

Ok, so you can also simple follow bellow full email Or also you can check demo.

Example:

<!DOCTYPE html>

<html>

<head>

<title>Get gravatar image from email Jquery</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript" src="http://demo.itsolutionstuff.com/plugin/jquery.md5.js"></script>

<style type="text/css">

.img{

width: 100px;

}

</style>

</head>

<body>


<div class="container">

<h2>Get gravatar image from email jquery</h2>

<form>

<div class="form-group">

<label>Email:</label>

<input type="email" name="email" class="form-control email" required>

</div>

<div class="form-group">

<img src="" class="img" >

</div>

<div class="form-group">

<button class="btn btn-success set-gravtar">Get Gravatar!</button>

</div>

</form>

</div>


<script type="text/javascript">

$(".set-gravtar").click(function(e){

e.preventDefault();

var email = $(".email").val();


if(email != ''){

var md5 = $.md5(email);

var imgUrl = 'https://gravatar.com/avatar/'+md5+'?&d=404';

$.ajax({

url:imgUrl,

type:"HEAD",

crossDomain:true,

error:function(){

$(".img").attr("src","http://itsolutionstuff.com/frontTheme/images/logo.png");

},

success:function(){

$(".img").attr("src",imgUrl);

}

});

}else{

alert('Please enter email.');

}


});

</script>


</body>

</html>

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

JQuery Moment Add Hours to Datetime Example

Read Now →

JQuery Moment Subtract Minutes to Datetime Example

Read Now →

JQuery Moment Subtract Days to Date Example

Read Now →

How to Use Jquery Datatable in Angular?

Read Now →

JQuery - How to Count Child Elements in Div?

Read Now →

How to Get Number of Characters in a String in JQuery?

Read Now →

JQuery Open Link in New Tab on Click Example

Read Now →

JQuery Open Link in New Window Example

Read Now →

How to Allow Only 4 Digits Numbers in Textbox using JQuery?

Read Now →

How to Get Unique Values from Array in JQuery?

Read Now →

JQuery Check If Value Exists or Not in Array Example

Read Now →

How to Copy Text to Clipboard without Flash using JQuery?

Read Now →

JQuery Animated Typing Effect using Typed JS Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →