Connecting Tech Pros Worldwide Help | Site Map

Does Split() work in Mozilla?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 10:42 AM
Brian Glen Palicia
Guest
 
Posts: n/a
Default 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="JavaScript">
<!--

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="javascript:one()" type=button value="Does split() work
in Mozilla?"></INPUT><BR>

</FORM>

</BODY>
</HTML>

  #2  
Old July 23rd, 2005, 10:42 AM
Michael Winter
Guest
 
Posts: n/a
Default Re: Does Split() work in Mozilla?

On 4 May 2004 05:10:32 -0700, Brian Glen Palicia <bpalicia@nls.net> 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="JavaScript">[/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="javascript: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.getElementById( '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@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
  #3  
Old July 23rd, 2005, 10:42 AM
Brian Palicia
Guest
 
Posts: n/a
Default 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!
  #4  
Old July 23rd, 2005, 10:42 AM
Mick White
Guest
 
Posts: n/a
Default 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(formTextObject) {
return formTextObject.value.split(" ").join("\n");
}

<textarea id="output" rows="5" cols="80"></textarea><br>
<button type="button" onclick =
"alert(getWords(document.getElementById('output')) )">
Test split()
</button>
  #5  
Old July 23rd, 2005, 11:14 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Does Split() work in Mozilla?

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

That's not downwards compatible. Use this instead:

<form action="" onsubmit="return false">
<textarea name="output" rows="5" cols="80"></textarea><br>
<input
type="button"
value="Test split()"
onclick="alert(getWords(this.form.elements.output) )">
</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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.