Skip to content

Instantly share code, notes, and snippets.

@nealrs
Created May 5, 2014 21:32
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nealrs/28dbfe2c74dfdde26a30 to your computer and use it in GitHub Desktop.
Save nealrs/28dbfe2c74dfdde26a30 to your computer and use it in GitHub Desktop.
use the giphy api to search for & add an animated gif to a webpage.
// on page load, search for & display a random gif matching your search term using the Giphy API.
// usage:
// include giphy.js in your <head>
// set q to your search term (e.g. "brunch")
// add <span id = "giphyme"></span> wherever you want to display the image. -- FYI, it will be centered.
// big ups to the Giphy crew (giphy.com)
// 2014 - Neal Shyam [@nealrs | nealshyam.com]
document.addEventListener('DOMContentLoaded', function () {
q = "finger guns"; // search query
request = new XMLHttpRequest;
request.open('GET', 'http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag='+q, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400){
data = JSON.parse(request.responseText).data.image_url;
console.log(data);
document.getElementById("giphyme").innerHTML = '<center><img src = "'+data+'" title="GIF via Giphy"></center>';
} else {
console.log('reached giphy, but API returned an error');
}
};
request.onerror = function() {
console.log('connection error');
};
request.send();
});
// minimal demo
//<html>
//<head>
// <title>giphy.js demo</title>
// <script src="giphy.js"></script>
//</head>
//<body>
// <span id = "giphyme"></span>
//</body>
//</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment