Click to See Complete Forum and Search --> : Long strings ?
pmugghc
12-21-2002, 11:44 PM
I've checked bot the reference manual and the guide, but can't find what limitations there are on the length of a string. Tried to assign over multiple lines, but it seems that doesn't work.
How do you assign multiple lines of text to a string (literal or object). except using lots of multiple small strings and then concatenating ?
Thanks, Roland
ShrineDesigns
12-22-2002, 01:11 AM
a string can contain 255 characters or less
an easy way to compile strings is:
var str = myStringA + myStringB;
or
var str = myStringC + "some text to add to string";
or
str = new Array();
str[0] = document.myForm.myText.value;
str[1] = myStringA;
str[2] = "some text";
var myStr = str[0] + str[1] + str[2];
Charles
12-22-2002, 05:57 AM
a string can contain 255 characters or lessThat's true of Pascal but not of JavaScript. Try running:
<script type="text/javascript">
<!--
var s = '';
for(i=0;i<510;i++) {s+='x'}
alert (s.length);
// -->
</script>
I believe that the max length for a string is roughly 1.79E+308 in JavaScript.
ShrineDesigns
12-22-2002, 06:08 AM
what is 1.79E+308 ???
Charles
12-22-2002, 06:41 AM
1790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000
ShrineDesigns
12-22-2002, 06:44 AM
ok lol
pmugghc
12-22-2002, 06:56 AM
OK< that's great, so how can I do assignments of long strings, over multiple lines ?
Thanks, Roland
Charles
12-22-2002, 07:23 AM
<script type="text/javascript">
<!--
s = '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed'
s += 'diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam'
s += 'erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci'
s += 'tation ullamcorper suscipitlobortis nisl ut aliquip ex ea commodo'
s += 'consequat. Duis autem vel eum iriure dolor in hendrerit in'
s += 'vulputate velit esse molestie consequat, vel illum dolore eu'
s += 'feugiat nulla facilisis at vero eros et accumsan et iusto odio'
s += 'dignissim qui blandit praesent luptatum zzril delenit augue duis'
s += 'dolore te feugait nulla facilisi. Nam liber tempor *** soluta nobis'
s += 'eleifend option congue nihil imperdiet doming id quod mazim'
s += 'placerat facer possim assum.</p>'
document.write(s)
// -->
</script>
or
<script type="text/javascript">
<!--
document.write ('<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed',
'diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam',
'erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci',
'tation ullamcorper suscipitlobortis nisl ut aliquip ex ea commodo',
'consequat. Duis autem vel eum iriure dolor in hendrerit in',
'vulputate velit esse molestie consequat, vel illum dolore eu',
'feugiat nulla facilisis at vero eros et accumsan et iusto odio',
'dignissim qui blandit praesent luptatum zzril delenit augue duis',
'dolore te feugait nulla facilisi. Nam liber tempor *** soluta nobis',
'eleifend option congue nihil imperdiet doming id quod mazim',
'placerat facer possim assum.</p>')
// -->
</script>
ca_redwards
12-23-2002, 05:26 PM
Long JavaScript strings are what make dynamic client-side HTML generation of entire pages possible. In fact, I've created 154 JavaScript functions (in less than 2K!) that do this very efficiently.
Many JavaScripters are familiar with the .bold() function that surrounds a string's content with start/end bold tags. I took this concept to an extreme where entire pages can be created by chaining these functions onto strings and arrays of strings.
My HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html) allows relatively small bits of javascript code to build huge HTML strings, including entire web pages (see the last example there).
To avoid duplicating dom names, I named these functions in all uppercase. And for HTML construction simplicity, these functions are named like the HTML tag/attributes that they implement.
Charles
12-23-2002, 06:39 PM
And no doubt you pages fail completely for the 12% of users who eschew JavaScript.