Does Split() work in Mozilla?

  • Thread starter Brian Glen Palicia
  • Start date
B

Brian Glen Palicia

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 + "<br>");
}

}

//-->

</SCRIPT>
<BODY>

<FORM name="oneForm">

<INPUT onclick="javascript:eek:ne()" type=button value="Does split() work
in Mozilla?"></INPUT><BR>

</FORM>

</BODY>
</HTML>
 
M

Michael Winter

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.

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:

<HTML>
<HEAD>

Valid HTML documents must include a TITLE element.
</HEAD>
<SCRIPT language="JavaScript">

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


The practice of script hiding is obsolete. Remove the SGML comment
delimiters.
function one()
{

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

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).
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 + "<br>");
}


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.

Remove this.
</SCRIPT>
<BODY>

<FORM name="oneForm">

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

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.
</FORM>

</BODY>
</HTML>

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
 
B

Brian Palicia

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:
 
M

Mick White

Brian said:
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:


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>
 
T

Thomas 'PointedEars' Lahn

Mick said:
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>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top