NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
Home / News / Webmaster / Tips and Tricks

Tips and Tricks


Random Webpages

An Introduction To JavaScript Arrays

By Catalin Bocanu, Web News Editor

25th of April 2008, 15:48 GMT

Adjust text size:



Enlarge picture
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


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])
}






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.

TAGS:

JavaScript Tips | JavaScript Arrays | JavaScript Random Events | Programming Tutorials | Javascript Programming
Read by 2,163 user(s) | Add comment | Link to this article TWEET THIS


Article rating:
Good (3.1/5) 8 vote(s)    

Subscribe to news | Print article | Send to friend

© Copyright 2001-2009 Softpedia
Contact:

 

 

SEARCH THE NEWS ARCHIVE :




Today's News
| Yesterday's News | News Archive


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

User opinions:

No user comments yet.
Be the first to express your opinion using the form below!

Share your opinion:

Your Name:
Your Email Address:
(will not be used for commercial purposes)
Solve this to prove you're not a bot: =
Your review/opinion:

 




Windows tabGames tabDrivers tabMac tabLinux tabScripts tabMobile tabHandheld tabGadgets tabNews tab

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   ENTER NEWS SITE   |   ENGLISH BOARD   |   ROMANIAN FORUM