-
-
Save LensPlaysGames/bece0a4c98db5b778790f90aac867972 to your computer and use it in GitHub Desktop.
Show an random image from a subreddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Place in your body: | |
// <div id="randomimg"></div> | |
$(function () { | |
GetRandRedditImage("Pics"); | |
function GetRandRedditImage(subreddit) { | |
var imgcontainer = $("#randomimg"); | |
var aRandomNum = Math.floor((Math.random() * 25) + 1); | |
$.getJSON('http://www.reddit.com/r/' + subreddit + '.json?jsonp=?&show=all&limit=25', function (data) { | |
$.each(data.data.children, function (i, item) { | |
if (i == aRandomNum) { | |
if (item.data.post_hint == "image") { | |
console.log(item.data); | |
imgcontainer.html($("<img>", { src: item.data.url, class: 'imgkeepaspect' })); | |
return false; | |
} else { | |
GetRandRedditImage(subreddit); | |
} | |
} | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The dollar sign is commonly used as a shortcut to the function
document.getElementById()
, and is a function found in many popular libraries in JavaScript. In this case it comes from jQuery, but you could also just implement it yourself by doing something likefunction $(x) {return document.getElementById(x);}