JavaScript
  Home arrow JavaScript arrow Page 4 - JavaScript Arrays
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Arrays
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 83
    2005-05-25

    Table of Contents:
  • JavaScript Arrays
  • Multidimensional Arrays and Use
  • Beginning Code
  • Associating Images and Links
  • What to Put into the HTML

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    JavaScript Arrays - Associating Images and Links


    (Page 4 of 5 )

    The question now is, how do we ensure that each image is associated with the correct link? Simple, we use another set of arrays to hold each of the links, and use the same random number that was generated for the image to tie the correct link to each of the images that are displayed:

    links1 = new Array
    ("gaintek.htm","gfarce.htm","leadward.htm",
    "matrax.htm",
    "nvideon.htm")

    links2 = new Array("abyte.htm","dfrock.htm","epoch.htm","gigasus.htm",
    "saltech.htm")

    links3 = new Array("datastore.htm","maxgate.htm","sambell.htm",
    "toshachi.htm",
    "wmax.htm")

    links4 = new Array("amg.htm","amg32.htm","amg65.htm","nuetrino.htm",
    "outel.htm")

    links5 = new Array("corstan.htm","critical.htm","generic.htm",
    "kingmos.htm",
    "twinsung.htm")

     

    So, if randomNum1 equals 3, the script will display deals1[3] (images/gcards/leadward.gif) and links1[3] (leadward.htm), both of which are the correct values. This is why each link needs to be at the same index as the image it is paired with in each of the arrays. To actually make the links work, we need to create some more functions (one for each of the product lines). Add the following code chunk:

    function newLocation1() {

     document.location.href = links1[randomNum1]

    All this does is set the URL of the page to the correct link. The function will be called when the image is clicked on, which means we will need to add some code to the HTML document as well, which will be explained shortly.

    Next, we need to think about the roll-overs. Again, we need to ensure that the correct roll-over image is coupled with its counterpart. Some more code needs to be nestled among the HTML on the page itself, but we can set the array in the .js file:

    dealsover1 = new Array("images/gcards/gaintek-
    over.gif","images/gcards/gfarce-
    over.gif","images/gcards/leadward-
    over.gif","images/gcards/matrax-
    over.gif","images/gcards/nvideon-over.gif")

    dealsover2 = new Array("images/mobos/abyte-
    over.gif","images/mobos/dfrock-
    over.gif","images/mobos/epoch-
    over.gif","images/mobos/gigasus-
    over.gif","images/mobos/saltech-over.gif")

    dealsover3 = new Array("images/hdds/datastore-
    over.gif","images/hdds/maxgate-
    over.gif","images/hdds/sambell-
    over.gif","images/hdds/toshachi-
    over.gif","images/hdds/wmax-over.gif")

    dealsover4 = new Array("images/cpus/amg-
    over.gif","images/cpus/amg32-
    over.gif","images/cpus/amg65-
    over.gif","images/cpus/nuetrino-
    over.gif","images/cpus/outel-over.gif")

    dealsover5 = new Array("images/ram/corstan-
    over.gif","images/ram/critical-
    over.gif","images/ram/generic-
    over.gif","images/ram/kingmos-
    over.gif","images/ram/twinsung-over.gif")

     

    That is all that needs to be done with the JavaScript file, so save it with a .js extension and open a new text file. 

    More JavaScript Articles
    More By Dan Wellman


       · ...you may want to rename mySum to myProduct, since that's the value being...
       · Iteration over indexed and associative arrays would've been a big help.
       · Arrays are zero-indexed. myArray[1] is 89, not 15.Just like every other language...
       · Is not a multidimensional array, but an array of records, or structs (in c#).For...
       · document.write(myArray[1])would write 89 not 15Arrays in JavaScript are zero...
     

    JAVASCRIPT ARTICLES

    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway