By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,926 Members | 1,809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,926 IT Pros & Developers. It's quick & easy.

Does Split() work in Mozilla?

P: n/a
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>
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 4 May 2004 05:10:32 -0700, Brian Glen Palicia <bp******@nls.net> wrote:
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:

<URL:http://www.w3.org/TR/html401/struct/global.html#h-7.2>
<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

<script type="text/javascript">
<!--
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[i] + "<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:one()" 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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2

P: n/a
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!
Jul 23 '05 #3

P: n/a
Brian Palicia wrote:
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>
Jul 23 '05 #4

P: n/a
Mick White wrote:
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.