www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Passing array as function parameter in FF and MSIE

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    resolved [RESOLVED] Passing array as function parameter in FF and MSIE

    Can someone explain why this code works in FF, but fails in MSIE?
    In particular the content / display of the <select> options.

    What is different in the executions of the same script on different browsers?

    What can I do or change to make it work in both browsers?

    Code:
    <html>
    <head>
    <title> Pass array by reference </title>
    <script type="text/javascript">
    
    var ClassOf2010 = [
      'John Jacob Jingleheimer Smith',
      'Alice Wonderland',
    // ... etc ...
      'Seymore Butts'
    ];
    var ClassOf2011 = [
      'Gasoline Alley',
      'Betty Boop',
      'Clarabel Cow',
      'Deputy Dog',
    // ... etc ...
      'Elmer Fudd'
    ];
    
    function showArray(List) { return List.join('\n'); }
    
    function $_(IDS) { return document.getElementById(IDS); }
    
    function AddOptionItem(IDS,Item) {
      Item.trim();
      var sel = $_(IDS);
      var tmp = Item.split(':');
      sel.options[sel.options.length]=new Option(tmp[0],Item,true,true);
    }
    
    function initStudentSBox(IDS,List) { 
      $_(IDS).options.length = 0;
      $_(IDS).options[0]=new Option('Choose',':blank',true,true);
      for (var i=0; i<List.length; i++) { AddOptionItem(IDS,List[i]+':'+i); }
      $_(IDS).selectedIndex = 0;
    }
    
    onload = function() {
      initStudentSBox('Studs2010',ClassOf2010);
      initStudentSBox('Studs2011',ClassOf2011);
    }
    
    </script>
    </head>
    <body>
    <button onclick="alert(showArray(ClassOf2010))">Show Class of 2010</button>
    <select id="Studs2010" onchange="alert(this.value)"></select>
    <p>
    <button onclick="alert(showArray(ClassOf2011))">Show Class of 2011</button>
    <select id="Studs2011" onchange="alert(this.value)"></select>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Even the I.E. error console makes it pretty clear: String.trim is undefined.
    Just add this:
    Code:
    if( String().trim === undefined )
     String.prototype.trim = function()
     {
      return this.replace( /^\s+|\s+$/g, "" );
     }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Thank you.

    I got so caught up into thinking it was a browser problem,
    I forgot to check!

    I was looking at <!doc ... > tags and everywhere but the obvious.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles