Random image with link


/ Published in: JavaScript
Save to your folder(s)

The ideal use of this code would be to save the Javascript code as a .js file. Then use the HTML code at the bottom to call it wherever you want on the web page.


Copy this code and paste it in your HTML
  1. <!-- Javascript code begins -->
  2.  
  3. var randomlinks=new Array()
  4.  
  5. randomlinks[0]='<img alt="" src="images/image-01.gif" width="150" height="75">'
  6. randomlinks[1]='<img alt="" src="images/image-02.gif" width="150" height="75">'
  7. randomlinks[2]='<img alt="" src="images/image-03.gif" width="150" height="75">'
  8.  
  9. function randomlink(){
  10. var add = randomlinks[Math.floor(Math.random()*randomlinks.length)]
  11. return(add)
  12. }
  13.  
  14. document.write( randomlink() );
  15.  
  16. <!-- Javascript code ends -->
  17.  
  18.  
  19.  
  20. <!-- HTML code to use script -->
  21.  
  22. <script type="text/javascript" src="path/to/script.js"></script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.