Skip to content

Instantly share code, notes, and snippets.

@d33pfri3d
Created July 9, 2012 06:20
Show Gist options
  • Save d33pfri3d/3074506 to your computer and use it in GitHub Desktop.
Save d33pfri3d/3074506 to your computer and use it in GitHub Desktop.
Random IMGUR finder
function randomString(string_length)
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var output = "";
var index;
for (var i = 0; i < string_length; i++)
{
var index = Math.floor(Math.random() * chars.length);
output += chars.substring(index, index + 1);
}
return output;
}
function getImages()
{
var numTries = 1;
var imgObject = new Image();
var dots = "";
imgObject.onload = function()
{
if (this.width == 161 && this.height == 81)
{
this.src = "http://i.imgur.com/" + randomString(5) + ".jpg";
dots += ".";
document.getElementById('a').innerHTML = "Please wait..." + dots;
numTries++;
}
else
document.getElementById('a').innerHTML = "It took " + numTries + (numTries == 1 ? " try" : " tries") + " to get this picture. <a href=\"javascript:getImages();\">Load new image</a><br/><br/><a href=\"" + imgObject.src + "\">" + imgObject.src + "<br/><img src=\"" + imgObject.src + "\" /></a>";
}
// First image attempt that sets off the onload code defined above
imgObject.src = "http://i.imgur.com/" + randomString(5) + ".jpg";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment