JavaScript Document Code Not Cross Platform

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bradwiseathome@hotmail.com

    #1

    JavaScript Document Code Not Cross Platform


    I have code that works in IE 6 but does not work in FireFox 1.0.2, how
    can I change it so it works in both browsers?


    <html>
    <head>
    <script language="JavaS cript" type="text/JavaScript">
    function AttributeSelect ed()
    {
    var selectVal = document.forms['testspit'].elements['lsbRA'].options;
    if (selectVal.valu e == 'Item1')
    {
    document.getEle mentById('text2 ').style.visibi lity='visible';
    document.getEle mentById('spnla bel').style.vis ibility='visibl e';
    }
    else
    {
    document.getEle mentById('text2 ').style.visibi lity='hidden';
    document.getEle mentById('spnla bel').style.vis ibility='hidden ';
    }
    }
    </script>
    </head>
    <body>
    <form name="testspit" method="post" action="">
    <div id="divPlace" style="width:90 %;left:10px;top :40px">
    <select multiple id="lsbRA" onChange="Attri buteSelected(); " >
    <option value="Item1">I tem 1</option>
    <option value="Item2">I tem 2 </option>
    </select>
    <br/><br/>
    <input name="text1" type="text" value="text1" size="20">
    <br/><br/>
    <span id="spnlabel" style="overflow :hidden">
    -
    </span>
    <span id="spntext" style="overflow :hidden">
    <input name="text2" type="text" value="text2" size="20">
    </span>
    </div>
    </body>
    </html>


    Thanks.

  • RobG

    #2
    Re: JavaScript Document Code Not Cross Platform

    bradwiseath...@ hotmail.com wrote:[color=blue]
    > I have code that works in IE 6 but does not work in FireFox 1.0.2,[/color]
    how[color=blue]
    > can I change it so it works in both browsers?
    >
    >[/color]

    Missing DOCTYPE - that has ramifications here (see below), suggest:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

    [color=blue]
    > <html>
    > <head>
    > <script language="JavaS cript" type="text/JavaScript">[/color]

    The language attribute is depreciated, keep the required type
    attribute.
    [color=blue]
    > function AttributeSelect ed()
    > {
    > var selectVal =[/color]
    document.forms['testspit'].elements['lsbRA'].options;

    In quirksmode (sans DOCTYPE) IE handles this, Firefox doesn't. If
    lsbRA was a single select, then using the above suggested DOCTYPE with:

    var selectVal = document.forms['testspit'].elements['lsbRA'];

    will fix the issue if 'lsbRA' is a single select - take care, if the
    option has no 'value' attribute the value is supposed to be the text,
    but IE doesn't understand that.

    Adding '.options' on the end returns a collection of the options, which
    has no 'value' attribute (it is effectively an array). Removing
    '.options' means selectVal will be the element 'lsbRA', whose value
    will be that of the selected option.

    Since 'lsbRA' is a multiselect, you need to iterate through the options
    to find which ones are selected, you can't just get the value. Wrap
    your function with a loop that goes through all the options and deals
    with the selected ones, so now you have:

    var selectVal = document.forms['testspit'].elements['lsbRA'];
    for (var i=0, j=selectVal.len gth; i<j; i++){
    if (selectVal[i].selected){
    alert(selectVal[i].value + ' selected'); // Purely de-bug...
    ...

    [color=blue]
    > if (selectVal.valu e == 'Item1')[/color]

    becomes:

    if (selectVal[i].value == 'Item1')

    [color=blue]
    > {
    > document.getEle mentById('text2 ').style.visibi lity='visible';[/color]

    Here you are using getElementById on elements that have no ID. IE
    treats names and IDs as virtually the same thing, Firefox doesn't
    (because they are very different things but overlap somewhat in
    purpose). ID and NAME share the same namespace, so if you can't have
    different elements with the same name & id. You can, in some
    circumstances, have the same name on multiple elements.

    The quick 'n dirty solution here is to put the same ID on your elements
    as the name you've already given them.
    [color=blue]
    > document.getEle mentById('spnla bel').style.vis ibility='visibl e';
    > }
    > else
    > {
    > document.getEle mentById('text2 ').style.visibi lity='hidden';
    > document.getEle mentById('spnla bel').style.vis ibility='hidden ';
    > }[/color]

    Add a couple of curly braces to close the added for and if loops.

    }
    }
    [color=blue]
    > }
    > </script>
    > </head>
    > <body>
    > <form name="testspit" method="post" action="">
    > <div id="divPlace" style="width:90 %;left:10px;top :40px">
    > <select multiple id="lsbRA" onChange="Attri buteSelected(); " >
    > <option value="Item1">I tem 1</option>
    > <option value="Item2">I tem 2 </option>
    > </select>
    > <br/><br/>
    > <input name="text1" type="text" value="text1" size="20">[/color]

    <input name="text1" id="text1" ... >
    [color=blue]
    > <br/><br/>
    > <span id="spnlabel" style="overflow :hidden">
    > -
    > </span>
    > <span id="spntext" style="overflow :hidden">
    > <input name="text2" type="text" value="text2" size="20">[/color]

    <input name="text2" id="text2" ... >
    [color=blue]
    > </span>
    > </div>
    > </body>
    > </html>
    >
    >
    > Thanks.[/color]


    PS. The logic seems strange, but I'll leave that alone... unless it
    isn't doing whatever you expect it to. As it is, it may cause
    flickering for some users.


    --
    Rob

    Comment

    • Richard Cornford

      #3
      Re: JavaScript Document Code Not Cross Platform

      RobG wrote:[color=blue]
      > bradwiseath...@ hotmail.com wrote:[/color]
      <snip>[color=blue]
      > Missing DOCTYPE - that has ramifications here (see below),
      > suggest:
      >
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      > "http://www.w3.org/TR/html4/strict.dtd">[/color]
      <snip>[color=blue][color=green]
      >> </select>
      >> <br/><br/>[/color][/color]
      <snip>

      When the mark-up presented appears to XHTML what would suggest an HTML
      doctype?

      Richard.


      Comment

      • bradwiseathome@hotmail.com

        #4
        Re: JavaScript Document Code Not Cross Platform

        That is incredible. Thanks! I just learned a lot. The bottom line is
        that I need to show or hide a text box based on a selected option in a
        list, and it works in IE6 and FireFox 1.0.2, so I'm set.

        Thanks again!

        Comment

        • RobG

          #5
          Re: JavaScript Document Code Not Cross Platform

          Richard Cornford wrote:[color=blue]
          > RobG wrote:
          >[color=green]
          >>bradwiseath.. .@hotmail.com wrote:[/color]
          >
          > <snip>
          >[color=green]
          >>Missing DOCTYPE - that has ramifications here (see below),
          >>suggest:
          >>
          >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          >> "http://www.w3.org/TR/html4/strict.dtd">[/color]
          >
          > <snip>
          >[color=green][color=darkred]
          >>></select>
          >>><br/><br/>[/color][/color]
          >
          > <snip>
          >
          > When the mark-up presented appears to XHTML what would suggest an HTML
          > doctype?
          >[/color]

          I took a punt that the OP was really after HTML.

          It's probably reasonable to infer that the OP was attempting XHTML,
          but there seems to be an idea spreading that closing empty tags is
          desirable in HTML also - it will still validate OK as HTML 4 strict.

          There is also no character encoding, title or closing form tag.

          --
          Rob

          Comment

          • Michael Winter

            #6
            Re: JavaScript Document Code Not Cross Platform

            On 07/05/2005 02:14, RobG wrote:

            [snip]
            [color=blue]
            > there seems to be an idea spreading that closing empty tags is
            > desirable in HTML also - it will still validate OK as HTML 4 strict.[/color]

            It will validate, yes. However it means something entirely different.
            [color=blue]
            > There is also no character encoding [...][/color]

            To be fair to the OP, it is preferable to send the character encoding in
            the Content-Type HTTP header. You have no way of knowing if the charset
            parameter has been specified.

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Richard Cornford

              #7
              Re: JavaScript Document Code Not Cross Platform

              RobG wrote:
              <snip>[color=blue]
              > I took a punt that the OP was really after HTML.[/color]

              Given the IE doesn't understand XHTML, HTML would probably be the OP's
              best choice regardless of his intention.
              [color=blue]
              > It's probably reasonable to infer that the OP was
              > attempting XHTML, but there seems to be an idea
              > spreading that closing empty tags is desirable in
              > HTML also[/color]

              Many notions that are apparently not harmful are able to freely
              propagate. But HTML and XHTML are subject to detailed specification so
              they can be understood at a technical level, and they can both be
              objectively correct and incorrect. You will be hard pressed to find
              anyone with a technical understanding of these mark-up languages
              encouraging the mixing of the two styles.
              [color=blue]
              > - it will still validate OK as HTML 4 strict.[/color]

              That depends a great deal on the context of the use of the slash at the
              end of the tag. The SGML abbreviation '<br />' expands to '<br>>' ,
              where the second chevron is text content following the BR element. The
              abbreviated version will validate in contexts where text content would
              be allowed, but would not validate if the element-terminating slash
              implied the following text chevron in a context that did not allow text
              content. The strict DTD has fewer contexts in which text is allowed than
              the transition DTD.

              Regardless of whether this mixed mark-up validates, it is unlikely that
              the OP intended to insert a sequence of '>' characters into their page.
              The fact that the majority of browsers do not follow the HTML
              specification in this respect conceals the error, but it is still an
              error to create mark-up that should be expected to produce results other
              than those intended. And even browsers that do not handle the mark-up in
              the way that they really should still expend additional effort in
              disregarding what they perceive as a superfluous character in a tag.
              Thus the folly in mixing mark-up styles should be pointed out when it is
              observed, and discouraged.
              [color=blue]
              > There is also no character encoding, title or closing
              > form tag.[/color]

              As Mike said, character encoding is handled by required HTTP headers so
              the absence of an optional (and theoretically superfluous) META element
              is not an error. Other obvious mark-up errors could be pointed out to
              the OP, or the blanket recommendation to create valid mark-up provided
              (though the ill-conceived use of XHTML style mark-up in HTML would need
              to be pointed out separately as, as you observer, it will often
              validate).

              Richard.


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: JavaScript Document Code Not Cross Platform

                Richard Cornford wrote:
                [color=blue]
                > RobG wrote:[color=green]
                >> It's probably reasonable to infer that the OP was
                >> attempting XHTML, but there seems to be an idea
                >> spreading that closing empty tags is desirable in
                >> HTML also[/color]
                >
                > Many notions that are apparently not harmful are able to freely
                > propagate. But HTML and XHTML are subject to detailed specification so
                > they can be understood at a technical level, and they can both be
                > objectively correct and incorrect. You will be hard pressed to find
                > anyone with a technical understanding of these mark-up languages
                > encouraging the mixing of the two styles.[/color]

                <rant>
                Yes, I thought that, too, when reading the HTML Compatibility Guidelines
                of the XHTML 1.0 Specification. They are flawed for exactly that reason.
                </rant>


                PointedEars
                --
                There are two possibilities: Either we are alone in the
                universe or we are not. Both are equally terrifying.
                -- Arthur C. Clarke

                Comment

                • Jim Ley

                  #9
                  Re: JavaScript Document Code Not Cross Platform

                  On Sun, 15 May 2005 18:03:00 +0200, Thomas 'PointedEars' Lahn
                  <PointedEars@we b.de> wrote:
                  [color=blue]
                  ><rant>
                  >Yes, I thought that, too, when reading the HTML Compatibility Guidelines
                  >of the XHTML 1.0 Specification. They are flawed for exactly that reason.
                  ></rant>[/color]

                  The W3c is required to address all comments from the public, including
                  ones that arrive after it becomes a rec, rather than rant pointlessly
                  on usenet, www-html-editor@w3.org is the place to send your comments.

                  Jim.

                  Comment

                  Working...