Does Split() work in Mozilla?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Glen Palicia

    Does Split() work in Mozilla?

    My goal is to accept input from the user into a text box and then
    parse the data using split(). The first step is this tiny program to
    test the split() function. It runs in IE, but in Mozilla it just
    hangs and keeps loading forever. I checked around on the web and in
    USENET, but I haven't seen any mention of split() not working in
    Mozilla. Thoughts? Thanks in advance.

    <HTML>
    <HEAD>
    </HEAD>
    <SCRIPT language="JavaS cript">
    <!--

    function one()
    {

    var1 = "one, two, three, four"
    var2 = var1.split(",")

    document.write( "var1=" + var1 + "<br>");
    document.write( "var2 has " + var2.length + " elements:<br>") ;

    for (var i=0; i < var2.length; i++) {
    document.write( "Array Item #" + i + "=" + var2[i] + "<br>");
    }

    }

    //-->

    </SCRIPT>
    <BODY>

    <FORM name="oneForm">

    <INPUT onclick="javasc ript:one()" type=button value="Does split() work
    in Mozilla?"></INPUT><BR>

    </FORM>

    </BODY>
    </HTML>
  • Michael Winter

    #2
    Re: Does Split() work in Mozilla?

    On 4 May 2004 05:10:32 -0700, Brian Glen Palicia <bpalicia@nls.n et> wrote:
    [color=blue]
    > My goal is to accept input from the user into a text box and then
    > parse the data using split(). The first step is this tiny program to
    > test the split() function. It runs in IE, but in Mozilla it just
    > hangs and keeps loading forever. I checked around on the web and in
    > USENET, but I haven't seen any mention of split() not working in
    > Mozilla.[/color]

    That's because it does. Your code is the problem. I've included comments
    below. Most correct your HTML, which won't fix the problem, but you should
    heed anyway.

    HTML documents should include a document type declaration (DTD). See:

    <URL:http://www.w3.org/TR/html401/struct/global.html#h-7.2>
    [color=blue]
    > <HTML>
    > <HEAD>[/color]

    Valid HTML documents must include a TITLE element.
    [color=blue]
    > </HEAD>
    > <SCRIPT language="JavaS cript">[/color]

    There are two problems here:

    1) The SCRIPT element must either appear inside the HEAD or the BODY
    section. It is invalid anywhere else.
    2) The SCRIPT element requires the type attribute. Furthermore, the
    language attribute is deprecated and shouldn't be used anymore.

    Move the block into the HEAD section and replace the start tag above with

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    The practice of script hiding is obsolete. Remove the SGML comment
    delimiters.
    [color=blue]
    > function one()
    > {
    >
    > var1 = "one, two, three, four"
    > var2 = var1.split(",")[/color]

    You really should use better names. You should also declare these with the
    var keyword, otherwise they become global variables (which you don't want).
    [color=blue]
    > document.write( "var1=" + var1 + "<br>");
    > document.write( "var2 has " + var2.length + " elements:<br>") ;
    >
    > for (var i=0; i < var2.length; i++) {
    > document.write( "Array Item #" + i + "=" + var2[i] + "<br>");
    > }[/color]

    You should never use document.write( ) once a page has loaded. It causes
    the current information (including all JavaScript variables) to be trashed
    and the page is completely replaced. If you want to debug your code,
    either use an alert box, a TEXTAREA element, or the DOM to modify the page.
    [color=blue]
    > }
    >
    > //-->[/color]

    Remove this.
    [color=blue]
    > </SCRIPT>
    > <BODY>
    >
    > <FORM name="oneForm">[/color]

    A FORM element is only necessary in two instances:

    1) If you intend to submit data to a server.
    2) If you want to obtain references to form controls and you need to
    support old browsers

    You do neither here, so it's superfluous.
    [color=blue]
    > <INPUT onclick="javasc ript:one()" type=button value="Does split() work
    > in Mozilla?"></INPUT><BR>[/color]

    Two issues here:

    1) The INPUT element is empty. That is, it doesn't need, nor should it
    ever have, a closing tag.
    2) Specifiying "javascript :" in an intrinsic event is practially
    meaningless in all but one browser, and even then, it's unnecessary.
    Remove it.
    [color=blue]
    > </FORM>
    >
    > </BODY>
    > </HTML>[/color]

    Try this, and you'll see that Mozilla has no problems with split():

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

    <html lang="en" dir="ltr">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Script-Type" content="text/javascript">

    <title>Test</title>

    <script type="text/javascript">
    function one() {
    var output = document.getEle mentById( 'output' );

    var var1 = "one, two, three, four";
    var var2 = var1.split(",")

    output.value = "var1 = " + var1 + "\n";
    output.value += "var2 has " + var2.length + " elements:\n\n";

    for( var i = 0, n = var2.length; i < n; ++i ) {
    output.value += "Array Item #" + i + "=" + var2[ i ] + "\n";
    }
    }
    </script>
    </head>

    <body>
    <div>
    <textarea id="output" rows="5" cols="80"></textarea><br>
    <button type="button" onclick="one()" >Test split()</button>
    </div>
    </body>
    </html>

    Hope that helps,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Brian Palicia

      #3
      Re: Does Split() work in Mozilla?

      Wow. There was so much good information there, all of it really helped
      a lot. Thanks so much. Now I just need to figure out how to build a
      program to parse data from the textarea. :grin:

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Mick White

        #4
        Re: Does Split() work in Mozilla?

        Brian Palicia wrote:
        [color=blue]
        > Wow. There was so much good information there, all of it really helped
        > a lot. Thanks so much. Now I just need to figure out how to build a
        > program to parse data from the textarea. :grin:[/color]


        Use the same technique, just pass the textarea object to the function:

        function getWords(formTe xtObject) {
        return formTextObject. value.split(" ").join("\n ");
        }

        <textarea id="output" rows="5" cols="80"></textarea><br>
        <button type="button" onclick =
        "alert(getWords (document.getEl ementById('outp ut')))">
        Test split()
        </button>

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Does Split() work in Mozilla?

          Mick White wrote:
          [color=blue]
          > function getWords(formTe xtObject) {
          > return formTextObject. value.split(" ").join("\n ");
          > }
          >
          > <textarea id="output" rows="5" cols="80"></textarea><br>
          > <button type="button" onclick =
          > "alert(getWords (document.getEl ementById('outp ut')))">
          > Test split()
          > </button>[/color]

          That's not downwards compatible. Use this instead:

          <form action="" onsubmit="retur n false">
          <textarea name="output" rows="5" cols="80"></textarea><br>
          <input
          type="button"
          value="Test split()"
          onclick="alert( getWords(this.f orm.elements.ou tput))">
          </form>

          And don't forget to specify the script language for event handlers:

          <head>
          ...
          <meta http-equiv="Content-Script-Type" content="text/javascript">
          ...
          </head>


          PointedEars

          Comment

          Working...