Search Perform an advanced search query SOFTPEDIA
 
SOFTPEDIA
Updated one minute ago
HomeSubmit a program for being reviewedAdvertise on our websiteGet help on surfing our websitesSend us your feedbackGet information about our XML/RSS backend and how to use itBrowse the news archiveVisit our discussion forumVizitati forumul in limba romana



KLIP
  1. HOME
  2. SCIENCE
  3. TECHNOLOGY
  4. WEBMASTER
  5. SECURITY
  6. MICROSOFT
  7. LINUX
  8. APPLE
  9. GAMES
  10. TELECOMS
  11. REVIEWS
  12. LIFE & STYLE
  13. EDITORIALS
  14. INTERVIEWS
  15. RSS
Welcome!
Hello, Guest

Login if you have a Softpedia.com account.

Otherwise, register for one.

TIPS AND TRICKS

Random Webpages

- An Introduction To JavaScript Arrays

By: Catalin Bocanu, Web News Editor

Random events control will always be a challenge for any webmaster.
Depending on the event type, there are various coding options to produce a random action as output of a script. The manipulation of random web content can be performed from within any server side or client side programming language.

When you want to create a random navigation system that allows to open windows corresponding to different URLs, the simplest method is provided by a Javascript array implementation in a quick script.

The array represent a form of storage of multiple values in a single variable. By declaring variables values in an array there is no need to define a unique name for every variable. You can refer to a certain value contained in array by using its index.

Lets consider the next JavaScript example that will open a new browser window with a random webpage URL contained in an array:
CODE
<html>
<head>
<script language="JavaScript">
function randompage()
{
var arrindex=Math.floor(Math.random()*4)
var webpages=new Array(4)
webpages[0]="http://www.google.com"
webpages[1]="http://www.softpedia.com"
webpages[2]="http://webscripts.softpedia.com"
webpages[3]="http://news.softpedia.com"
window.open(webpages[arrindex])
}
</script>
</head>
<body>
<input type="button" value="Random Page" onClick="randompage()">
</body>
</html>


The line 7 uses the Array() function to declare the webpages array that will contain four elements. The next four lines defines the array elements (the pages URLs). By using the Array() function used without argument allows the insertion of undefined number of elements in the array. The arrindex variable value is randomly generated in the line 6: it will be an integer number situated in the range 0 and 3 because the generated numbers are rounded to the lower value of the closest integer in the range 0 and 3.99.

When the button called Random Page is pushed the randompage() function is called, a random number is generated and based on its value, the array index corresponding to a certain URL is established and in the new browser window opened the web page is shown. The script can be customized in various ways.You can add other URLs by increasing the number of array elements or the script can be adapted to show random content in web pages, such as random quotes.



MORE RELATED ARTICLES: Get A Free Popular IDE For PHP The Free Super Mario Game: Only 14kB Free Download Jabber Clients Made With Javascript Elegant Solutions for Online Stores Automatic Text Translation with Javascript An Unwanted Trip To A Porn Website ? Short Introduction to AJAX Performance Benchmarking Methods and Tools for Webservers The Real Power of Yahoo! Widgets A Simple Javascript Example
 
Comments | Link here | Subscribe
Print | Send to friend
Today's News | Yesterday's News

Search:


25th April 2008, 15:48 GMT | Copyright (c) 2008 Softpedia | Contact:
Read by 933 user(s) | Rating: | 7 vote(s) so far | Cast your vote:
Random Webpages - USER OPINIONS




We are sorry, there are no opinions available for this article.






SHARE YOUR OPINION ABOUT Random Webpages

Since you are not logged on, your comments will have to be approved before being displayed.
Click here to login, or register.
Your Name:
Your Email:
Type in the result:
Your Opinion:
 


DO YOU WANT TO CONTACT US?  

If you have some comments or you want to send us some information you can send us an email directly to .
You can use the form below for the same purpose.
Your full name: (at least 3 characters)
Your email address: (at least 5 characters)
Message subject: (at least 5 characters)
Message text:
(at least 10 characters)
Type in the result:
 
 



© 2001 - 2008 Softpedia. All rights reserved.
Softpedia™ and Softpedia™ logo are registered trademarks of SoftNews NET SRL.
Copyright Information | Privacy Policy | Terms of Use | Contact Softpedia | Update your software | Archive