SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)

    Native js objects sometimes AWOL in IE

    I was using 'new Option()' to create <option>'s in a select element but every now and again we get 'Option' is undefined.

    Undefined!

    A reason to stick with the newer DOM alternatives or innerHTML.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markbrown4 View Post
    I was using 'new Option()' to create <option>'s in a select element but every now and again we get 'Option' is undefined.
    Do you have a test case?
    Tab-indentation is a crime against humanity.

  3. #3
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Well I suppose a test case would be a page with this in it.

    var option = new Option('Ex', 'ample', false);

    It happens rarely and I have no clue what triggers it.
    My point is that 'Option' should always be defined.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here's confirmation that it's a known bug in IE8
    http://www.eggheadcafe.com/software/...e-loading.aspx
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    interesting, the screenshot I have of the error is indeed IE8.

  6. #6
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @markbrown4

    Code:
    A reason to stick with the newer DOM alternatives or innerHTML.
    yep, that's going to be way easier than trying to use host objects in a cross-browser manner...

    the element constructor functions (Option, Form, etc.) are *ancient*. beyond that, they are not a part of the ECMAScript spec. even when several browser vendors all agree to implement a given host object/method (i.e. alert()), there is always the risk that they won't be available (in a certain browser, in the future, etc.)

    one solution i've used frequently when i have to use host objects is to "touch" them first, i.e.:

    Code:
    if (window.Option) {
       // Global variable 'Option' is defined, probably
       // 'safe-ish' to use
       var option = new Option();
    }
    be aware though that (typeof SomeHostObjectConstructor === "function") doesn't always work as expected; several host objects in several browsers return wacky types. hope that helps


Bookmarks

Posting Permissions

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