Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,577 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,171 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Link Randomizer

Displays a random link (or image, or paragraph, or anything from that matter!) on a web page.

Submitted By: spearfish
Actions:
Rating:
Views: 440

Language: JavaScript

Last Modified: April 2, 2008
Instructions: As always, this is formatted in a nice HTML document :-)

You will need to modify the array "links" to suit your needs.

Open up a script and call function disp_links(number) to bring in the links wherever you want them. The parameter is the number of links you want displayed. They will be brought in as they are listed in the array, so if you plan to pull multiple links, be sure to put a space or a after each of the elements of the array.

Any HTML you're storing as an array element must use single quotes.

Also, you can use ANYTHING with this script, not just links. Whatever HTML you store as an array element (including pictures, paragraphs, linked pictures) will be brought out to wherever the function is called from.

-Spear

Snippet


  1. <html>
  2. <head>
  3. <title>LINK_RAND</title>
  4. <script type='text/javascript'>
  5. var links=new Array();
  6. links[0]="<a href='http://www.yahoo.com/'>Yahoo</a> ";
  7. links[1]="<a href='http://www.dreamincode.net/'>Dream.In.Code</a> ";
  8. links[2]="<a href='http://www.example.com/'>Example.com</a> ";
  9. links[3]="<a href='http://www.conquerclub.com/'>Free online risk!</a> ";
  10. links[4]="<a href='http://www.google.com/'>The best search engine in the world</a> ";
  11. function disp_links(number) {
  12.         for (i=1;i<=number;i++) {
  13.                 var k = Math.floor(Math.random()*links.length);
  14.                 document.write(links[k]);
  15. }
  16.         }
  17. </script>
  18. </head>
  19. <body>
  20. <p><script type='text/javascript'>disp_links(1);</script></p>
  21. </body>
  22. </html>

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month