---
title: "Random Images From Wikipedia"
description: "After Random images from xkcd as wallpaper it was time for some more fun, this time did a simple script to get Random images."
url: "https://h3manth.com/scribe/random-images-wikipedia/"
canonical_url: "https://h3manth.com/scribe/random-images-wikipedia/"
markdown_url: "https://h3manth.com/scribe/blog/random-images-wikipedia.md"
publishDate: "2011-08-27T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","web"]
word_count: 389
reading_time_minutes: 2
x_markdown_tokens: 1068
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Random Images From Wikipedia

> After Random images from xkcd as wallpaper it was time for some more fun, this time did a simple script to get Random images.

After [Random images from xkcd as wallpaper](http://h3manth.com/content/random-images-xkcd-wallpaper) it was time for some more fun, this time did a simple script to get Random images from wikipedia!

Some simple **Jquery Ajax calls** to wikipedia did the trick of getting random images from wikipedia!

Check out the [DEMO!](http://h3manth.com/random-wiki-img.html)

**The code :**

The idea was pretty simple :

  * Select a random country from the countries array. 
  * For that select country, get 30 random images from wikipedia API. 
  * Out the 30 random images thus fetched, pick a random image and load it to a div 

function GetRandomCountry()  
{  
var country = "";  
var countries = new Array("Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burma", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Democratic Republic", "Congo, Republic of the", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Mongolia", "Morocco", "Monaco", "Mozambique", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda", "Samoa", "San Marino", " Sao Tome", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe");  
  
country = countries[Math.floor(Math.random() * countries.[length](http://docs.jquery.com/Core/length))];  
return country;  
}  
  
$(document).[ready](http://docs.jquery.com/Events/ready)(function() {  
$( "#random" ).[click](http://docs.jquery.com/Events/click)(function() {  
var rand_name = GetRandomCountry();  
var url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&ailimit=30&format=json&aifrom=" +rand_name+ "&callback=?";  
console.log(url);  
$.[getJSON](http://docs.jquery.com/Ajax/getJSON)( url, function( [data](http://docs.jquery.com/Core/data) ) {  
var rand_num = Math.floor(Math.random()*31);  
$("#rimg").[html](http://docs.jquery.com/Attributes/html)("<br> <img src= \""+[data](http://docs.jquery.com/Core/data).query.allimages[rand_num].url+" \" />");  
});  
});  
  
});

Enjoy the randomness of life! ^_^
