• Cross Browser JavaScript tips

    Author
    Topic
    #378222

    I recently discovered the answer to something that had baffled me for months and I thought that others might benefit from the same information.

    I usually do my web application development and testing exclusively with Microsoft products (Visual Studio 7 and IE 6). Occasionally, I might test some things with Netscape 4.79 or 6.2 to see if it works properly. There was always one technique that worked very easily in IE, but always failed in Netscape

    Viewing 1 reply thread
    Author
    Replies
    • #625930

      Mark, even though there hasn’t been a response….I think this information is second to none. Thanks a million for your endless hard work and contributions.

    • #626061

      Mark,

      That’s a great tip. In general, IE always seems to be a little “smarter” than Netscape. When I work with other developers that are not familiar with cross-browser development, it’s interesting to see little things like this tripping them up. Here’s another one that almost everyone gets “wrong”:

      If you’ve got a form with a SELECT element (a drop-down list), you would think that you can get the current value as follows:

      var x = document.form1.select1.value;

      And you’d be right… in IE (or Opera 6.x). But Netscape doesn’t use the value property on the select element – it’s always null. You need to use the complete reference to the value of the selected option, like this:

      var x = document.form1.select1.options[document.form1.select1.selectedIndex].value;

      Not very intuitive. Value can be a tricky attribute in other places too. A checkbox with an assigned value has that value whether or not it’s checked. When the form is submitted, the value gets returned only if the checkbox’s checked property is true.

      I wrote a getVal function that inspects an element to determine it’s type and returns the “current value” (what would show up in the data if the form was being submitted). It’s pretty straight-=forward for everything but radio button groups – those are just a real pain to identify. If anyone’s interested, I’ll start a thread with the code and maybe we can fix it. It’s very useful for pages where an element that’s normally displayed is sometimes a hidden field, not to mention simplifying the code for getting the current value of a SELECT element.

      • #626064

        Hi Charlie,
        That’s some great information! I’ve noticed some issues with the SELECT element’s ‘value’ property. I usually only test to make sure it’s not empty, but it’s nice to know you can read the actual value with the statement you provided.

        Checkboxes and Radio groups are often a pain – however, I usually find myself dealing with them on the Server-side (with ASP) rather than client side (with JavaScript).

        By all means, please feel free to share your function(s) – that would be GREAT!! joy

        Thanks

    Viewing 1 reply thread
    Reply To: Cross Browser JavaScript tips

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: