Javascript - Display Random Images On Load

Submitted by: 
Language: 
Visitors have accessed this post 272 times.

In this tutorial we will create a Display Random Images On Load using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting Started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will display the images to the body. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  9. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  10. </a> </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">Javascript - Display Random Images On Load</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <img id="target" height="400px" width="600px"/>
  16. </div>
  17. </body>
  18. <script src="js/script.js"></script>
  19. </html>

Creating the Script

This code contains the script of the application. This code will render a random images to the page body. To do this just copy write the code as shown below inside the text editor and save it as script.js.

  1. var images = [
  2. 'image 1.jpg',
  3. 'image 2.jpg',
  4. 'image 3.jpg'
  5. ];
  6.  
  7. var length = images.length;
  8. var rand = Math.floor(length*Math.random());
  9. document.getElementById('target').src="images/" + images[rand];

There you have it we successfully created a Display Random Images On Load using javascript. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.