Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Created May 18, 2016 11:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pixelbart/d34173e01e7f7357be6c3199010b389e to your computer and use it in GitHub Desktop.
Save pixelbart/d34173e01e7f7357be6c3199010b389e to your computer and use it in GitHub Desktop.
Show an random image from a subreddit
// Place in your body:
// <div id="image"></div>
$(document).ready( function() {
var subreddit = 'VillagePorn'; // Your Subreddit without /r/
var aRandomNum = Math.floor((Math.random() * 25) + 1); // A random number - range 25
$.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) {
$("<img/>").attr("src", item.data.url).appendTo("#image");
return false;
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment