Skip to content

Instantly share code, notes, and snippets.

@LensPlaysGames
Forked from pixelbart/subreddits.js
Last active August 2, 2021 19:27
Show Gist options
  • Save LensPlaysGames/bece0a4c98db5b778790f90aac867972 to your computer and use it in GitHub Desktop.
Save LensPlaysGames/bece0a4c98db5b778790f90aac867972 to your computer and use it in GitHub Desktop.
Show an random image from a subreddit
// 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);
}
}
});
});
}
});
@JustCurious4321
Copy link

It is likely a very strange question, but what is the definition of $ ?

@LensPlaysGames
Copy link
Author

LensPlaysGames commented Aug 2, 2021

It is likely a very strange question, but what is the definition of $ ?

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 like
function $(x) {return document.getElementById(x);}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment