On 4 May 2004 05:10:32 -0700, Brian Glen Palicia <bp******@nls.n et> 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="JavaS cript">
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="javasc ript: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.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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)