{"id":4476,"date":"2016-03-02T11:57:19","date_gmt":"2016-03-02T11:57:19","guid":{"rendered":"http:\/\/www.hostingnotes.com\/?p=4476"},"modified":"2017-10-09T14:39:33","modified_gmt":"2017-10-09T14:39:33","slug":"random-image-display-using-javascript-2","status":"publish","type":"post","link":"https:\/\/www.webdevelopersnotes.com\/random-image-display-using-javascript-2","title":{"rendered":"Random Image Display Using JavaScript"},"content":{"rendered":"<p>The steps involved in randomly displaying an image from a set are very similar to those used for displaying random text strings.<\/p>\n<ol class=\"slide-steps\">\n<li>Initializing an array<\/li>\n<li>Storing the image file names in that array<\/li>\n<li>Finding the length of this array<\/li>\n<li>Using the <em>Math.random()<\/em> to generate a random number<\/li>\n<li>Using the randomly generated number as index for retrieving an image file name from the array.<\/li>\n<li>Displaying the image.<\/li>\n<\/ol>\n<pre class=\"small-text\">\r\nvar img_name = new Array(\"purple.gif\", \"red.gif\",\r\n\"blue.gif\", \"yellow.gif\", \"green.gif\", \"pink.gif\");\r\n\r\nvar l = img_name.length;\r\n\r\nvar rnd_no = Math.floor(l*Math.random());\r\n\r\ndocument.r_img.src = img_name[rnd_no];\r\n\r\n&lt;img src=\"purple.gif\" width=\"100\" height=\"50\" \r\nname=\"r_img\" alt=\"Random image\" \/&gt;\r\n<\/pre>\n<p>In the code above, we have JavaScript array containing 6 images. The array length found from <span class=\"codeword\"><strong>length<\/strong><\/span> is, thus, 6 &#8211; img_name[0] would be &#8220;purple.gif&#8221;, img_name[1] would be &#8220;red.gif&#8221; and so on. We then use the <span class=\"codeword\">Math.round()<\/span> to get a random number, multiply it with the array length and round it up using <span class=\"codeword\">Math.floor()<\/span>.<\/p>\n<p>Note: Array are zero-indexed, so the last element in the array will the length of the array minus 1. However, since we are employing floor() to get an integer, we can use the value from <span class=\"codeword\">ARRAY_NAME.length<\/span> directly.<\/p>\n<p>You can also use the <span class=\"codeword\">id<\/span> attribute of the image tag instead of <span class=\"codeword\">name<\/span>; as:<\/p>\n<pre class=\"small-text\">\r\nvar img_name = new Array(\"purple.gif\", \"red.gif\",\r\n\"blue.gif\", \"yellow.gif\", \"green.gif\", \"pink.gif\");\r\n\r\nvar l = img_name.length;\r\n\r\nvar rnd_no = Math.floor(l*Math.random());\r\n\r\ndocument.getElementById(\"randomimage\").src = img_name[rnd_no];\r\n\r\n&lt;img src=\"purple.gif\" width=\"100\" height=\"50\" \r\nid=\"randomimage\" alt=\"Random image\" \/&gt;\r\n<\/pre>\n<h2>When can you display random images on a web page?<\/h2>\n<p>A potential use of the random image displaying code is when you want to show a single banner from a set &#8211; though I would prefer using server-side scripts like PHP or Perl for this. I have also employed the code to randomly rotate images at a designated place on the web page.<\/p>\n<div class=\"pagenav\">\n<ul>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/random-text-display-using-javascript\" title=\"random text display using javascript\">&lt; Back<\/a><\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-tutorial\" title=\"JavaScript tutorial\">1<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-event-handlers-part-1\" title=\"Javascript onmouseover and javascript onmouseout event handlers\">10<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-if-statement\" title=\"Javascript IF statement\">20<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/changing-images-on-mouseover-using-javascript\" title=\"Changing images on mouseover using Javascript\">30<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/random-image-display-using-javascript-2\" title=\"Random image display using javascript\" class=\"thispage\">35<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-programming-tutorial-last-words\" title=\"Completing the Javascript programming tutorial\">Next &gt;<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"The steps involved in randomly displaying an image from a set are very similar to those used for displaying random text strings. Initializing an array Storing the image file names in that array Finding the length of this array Using the Math.random() to generate a random number Using the randomly generated number as index for [&hellip;]","protected":false},"author":1,"featured_media":15661,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/comments?post=4476"}],"version-history":[{"count":0,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4476\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media\/15661"}],"wp:attachment":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media?parent=4476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/categories?post=4476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/tags?post=4476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}