Connecting Tech Pros Worldwide Help | Site Map

Parameterizing a string literal

whitesmith
Guest
 
Posts: n/a
#1: Mar 16 '07
Hi all,

A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:

function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
....
function printText24( text )
{
document.getElementById( "textLine24" ).innerHTML = text;
}

Is there a way to parameterize the string literal (textLineXX) so that
I can do it all by repeatedly calling one printText function?

-Lost
Guest
 
Posts: n/a
#2: Mar 16 '07

re: Parameterizing a string literal


"whitesmith" <apasserby@hushmail.comwrote in message
news:1174001410.506064.196040@y66g2000hsf.googlegr oups.com...
Quote:
Hi all,
>
A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:
>
function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
...
function printText24( text )
{
document.getElementById( "textLine24" ).innerHTML = text;
}
>
Is there a way to parameterize the string literal (textLineXX) so that
I can do it all by repeatedly calling one printText function?
Assuming I understand correctly, you can use something like:

var i = n; // where n represents an integer
document.getElementById('textLine' + i).innerHTML = text;

Wrapping it in a function, you could pass i, and text in as arguments. You could then
call the function in a loop. Adversely, you could write the function so that it contains
the loop, assuming text remains the same or is called from some predetermined value.

-Lost


Lee
Guest
 
Posts: n/a
#3: Mar 16 '07

re: Parameterizing a string literal


whitesmith said:
Quote:
>
>Hi all,
>
>A section of my webpage consists of two dozen lines of text displayed
>by sequentially calling:
>
>function printText1( text )
>{
> document.getElementById( "textLine1" ).innerHTML = text;
>}
>...
>function printText24( text )
>{
> document.getElementById( "textLine24" ).innerHTML = text;
>}
>
>Is there a way to parameterize the string literal (textLineXX) so that
>I can do it all by repeatedly calling one printText function?
You can concatinate a number to a string with the "+" operator:

function printText(N,text) {
document.getElementById("textLine"+N).innerHTML=te xt;
}

For general use, you'd want to add a test to ensure that
textLineN exists before trying to access its innerHTML
attribute, but this is good enough as long as you're careful.


--

whitesmith
Guest
 
Posts: n/a
#4: Mar 16 '07

re: Parameterizing a string literal


On Mar 15, 7:56 pm, "-Lost" <missed-s...@comcast.netwrote:
Quote:
"whitesmith" <apasse...@hushmail.comwrote in message
>
news:1174001410.506064.196040@y66g2000hsf.googlegr oups.com...
>
>
>
Quote:
Hi all,
>
Quote:
A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:
>
Quote:
function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
...
function printText24( text )
{
document.getElementById( "textLine24" ).innerHTML = text;
}
>
Quote:
Is there a way to parameterize the string literal (textLineXX) so that
I can do it all by repeatedly calling one printText function?
>
Assuming I understand correctly, you can use something like:
>
var i = n; // where n represents an integer
document.getElementById('textLine' + i).innerHTML = text;
>
Wrapping it in a function, you could pass i, and text in as arguments. You could then
call the function in a loop. Adversely, you could write the function so that it contains
the loop, assuming text remains the same or is called from some predetermined value.
>
-Lost
Thanks Lost and Lee! My background is C++ which would make menacing
complaints to anyone who tried to add an integer to a string.

-Lost
Guest
 
Posts: n/a
#5: Mar 16 '07

re: Parameterizing a string literal


"whitesmith" <apasserby@hushmail.comwrote in message
news:1174009618.096654.99780@e65g2000hsc.googlegro ups.com...
Quote:
On Mar 15, 7:56 pm, "-Lost" <missed-s...@comcast.netwrote:
Quote:
>"whitesmith" <apasse...@hushmail.comwrote in message
>>
>news:1174001410.506064.196040@y66g2000hsf.googleg roups.com...
Quote:
>>>
A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:
>>
Quote:
function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
...
function printText24( text )
{
document.getElementById( "textLine24" ).innerHTML = text;
}
>>
Quote:
Is there a way to parameterize the string literal (textLineXX) so that
I can do it all by repeatedly calling one printText function?
>>
>Assuming I understand correctly, you can use something like:
>>
>var i = n; // where n represents an integer
>document.getElementById('textLine' + i).innerHTML = text;
>>
>Wrapping it in a function, you could pass i, and text in as arguments. You could then
>call the function in a loop. Adversely, you could write the function so that it
>contains
>the loop, assuming text remains the same or is called from some predetermined value.
>>
>
Thanks Lost and Lee! My background is C++ which would make menacing
complaints to anyone who tried to add an integer to a string.
Not a problem. See:

http://www.jibbering.com/faq/faq_not....html#tcString

....for further understanding.

-Lost


Dr J R Stockton
Guest
 
Posts: n/a
#6: Mar 17 '07

re: Parameterizing a string literal


In comp.lang.javascript message <etcqc90rr7@drn.newsguy.com>, Thu, 15
Mar 2007 18:03:05, Lee <REM0VElbspamtrap@cox.netposted:
Quote:
>whitesmith said:
Quote:
Quote:
>>A section of my webpage consists of two dozen lines of text displayed
>>by sequentially calling:
>>
>>function printText1( text )
>>{
> document.getElementById( "textLine1" ).innerHTML = text;
>>}
>>...
>>function printText24( text )
>>{
> document.getElementById( "textLine24" ).innerHTML = text;
>>}
>>
>>Is there a way to parameterize the string literal (textLineXX) so that
>>I can do it all by repeatedly calling one printText function?
>
>You can concatinate a number to a string with the "+" operator:
You can concatenate anything to a string with the binary + operator -
for example "xx " + (1==1) "xx " + new Date() "xx " + isNaN
"xx " + [3,5] "xx" + {}

Quote:
>function printText(N,text) {
document.getElementById("textLine"+N).innerHTML=te xt;
>}
In any code in which document.getElementById( ).innerHTML = is
written more than two or three times, it would be better to use a
function for it :
function Wryt(ID, S) { document.getElementById(ID).innerHTML = S }
or DynWrite from the FAQ.


It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
=?ISO-8859-1?Q?G=E9rard_Talbot?=
Guest
 
Posts: n/a
#7: Mar 17 '07

re: Parameterizing a string literal


whitesmith wrote :
Quote:
Hi all,
>
A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:
>
function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
On top of what Lee said...

You should not use innerHTML to replace a text node. This is slower and
more memory demanding than childNodes[0].nodeValue or
firstChild.nodeValue or childNodes[0].data or even replaceData(offset,
count, arg in DOMString)

Speed and Performance comparison between innerHTML attribute and DOM's
nodeValue
http://www.gtalbot.org/DHTMLSection/...NodeValue.html

Interactive DOM level 2 CharacterData Interface attributes and methods tests
http://www.gtalbot.org/DHTMLSection/...acterData.html

Alternatives to innerHTML
http://slayeroffice.com/articles/inn..._alternatives/

innerHTML VS DOM
http://www.developer-x.com/content/innerhtml/

Gérard
--
Using Web Standards in your Web Pages (Updated Dec. 2006)
http://developer.mozilla.org/en/docs...your_Web_Pages
Dr J R Stockton
Guest
 
Posts: n/a
#8: Mar 18 '07

re: Parameterizing a string literal


In comp.lang.javascript message <12voa7litf9dh5a@corp.supernews.com>,
Sat, 17 Mar 2007 13:44:29, Gérard Talbot <newsblahgroup@gtalbot.org>
posted:
Quote:
>On top of what Lee said...
>
>You should not use innerHTML to replace a text node. This is slower and
>more memory demanding than childNodes[0].nodeValue or
>firstChild.nodeValue or childNodes[0].data or even replaceData(offset,
>count, arg in DOMString)
>
>Speed and Performance comparison between innerHTML attribute and DOM's
>nodeValue
>http://www.gtalbot.org/DHTMLSection/...NodeValue.html
The page claims "at least 200% slower on ... Internet Explorer 6+" - in
IE6 and IE7 (on differing machines), I get only 50% slower - times
typically 125 & 187.

But the nodeValue code could I think be made faster by changing (I've
shortened identifiers; untested) from

{
if(a[i].childNodes[0] && a[i].childNodes[0].nodeType == 3)
{
a[i].childNodes[0].nodeValue = "Now ... nodeValue";
};
};
to
{
if ( (T=a[i].childNodes[0]) && T.nodeType == 3)
{
T.nodeValue = "Now ... nodeValue";
};
};

Comparison of speeds for changing arbitrary items (not sequentially
named, for example) could be useful in addition, preferably using a
function such as Wryt(ID, Str) {} but with different bodies.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread