st***********@yahoo.co.uk wrote:
Hi People wondering if anyone can help me with a problem I'm having
I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code that I have
what I have so far is a page that has a text box for user input, which
will take whatever the user enters, will count all the characters in
the inputted text which also then uses the "split" command, to split
the users text (string) into its individual pieces.
the code I have so far is as follows
<html>
<head>
<script type="text/javascript">
function myfunction()
{
var v = document.form1.Text_Box.value;
var chop =v;
var x = v.length;
var parts=chop.split("");
'parts' is now an array of the individual characters of 'chop'.
Incidentally, the length of 'parts' will be the same as v.length, i.e.
the value you assigned to 'x'. Now it is available in 3 places, x seem
redundant.
var winPtr = window.open('', '_blank', '...features...');
var str = '<html>\n';
When writing a document, you should make sure you write valid HTML,
including a doctype. You can omit the head and body tags if you like,
they are optional.
str += '<head>\n';
str += '<title>Dynamic Page</title>\n';
str += '</head>\n';
str += '<body>\n';
str += '<p>\n';
str += v + '\n';
str += '<br>\n';
str += '<br>\n';
str += x + '\n';
str += '<br>\n';
str += '<br>\n';
str += parts + '\n';
str += '</p>\n';
str += '</body>\n';
str += '</html>';
String concatenation using the '+=' compound operator is notoriously
slow in some browsers, though you won't notice it here. You could put
all this in an array:
var str = [
'<html>',
'<head>',
'<title>Dynamic Page<\/title>',
...
'<\/body>',
'<\/html>'
];
Note all the newlines are removed (less to type :-) ). Then write it with:
winPtr.document.write(str.join('\n'));
Adding newlines will just make the HTML look better when you use view
source, otherwise they aren't necessary.
A joined array may not be faster in all browsers, but it is in most. If
nothing else, it is less to type. The forward-slash character should be
quoted when it follows '<' when using document.write.
winPtr.document.open();
There is no need to call document.open(), it is called automatically
with document.write().
winPtr.document.writeln(str);
winPtr.document.close();
}
</script>
</head>
<body>
<form name="form1">
Form elements require an action attribute, even if it's empty.
<TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>
There is no HTML value attribute for a textarea, its value is its text
content.
<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-TEXTAREA>
<input type="button"
onclick="myfunction()"
value="Font 1">
</form>
</body>
</html>
I'm aiming to perform some comparisons on the arrays elements, once
I've overcome this hurdle!
What hurdle? What are you trying to do? There is almost never any need
to declare the length of a JavaScript array, it is self-adjusting. Just
insert elements wherever you want:
// Initialise an array named 'A'
var A = [];
// Give the 6th element a value
A[5] = 'blah';
After step 1, A has a length of 0. After adding an element at index 5,
it has a length of 6 - the length property of an array is special, it is
always larger than the highest index that has been given a value and it
adjusts automatically. Read the ECMAScript Language specification
Section 15.4, particularly section 15.4.4 Properties of the Array
Prototype Object, it is quite readable.
JavaScript arrays are also sparse, that is, they only take up memory for
the elements that have been given a value. Search the archives, there
have been some long and detailed threads on arrays within the last 6 months.
--
Rob