View Full Version : Document.write other
bluewalrus
11-02-2009, 03:04 AM
Is there a javascript function like echo in php to put the contents of a javascript value? I can't use the document.write because it is being called in a function (function's not the correct term above, what are those called?) that loads multiple times.
I'm trying to output this full array if that makes a different.
a = a + 1;
list_of_files[a] = file.name;
I figured putting whatever outputs the result in a while loop like this would display all the contents or is there an output array contents function (function phrased used wrong again).
while (a < 0 ) {
codetowrite(list_of_files[a]);
a=a-1;
}
Thanks.
It's called a while statement. Make an array called data, and in the while statement, add the information to string within that array.
Good luck!
jscheuer1
11-02-2009, 06:15 AM
The document.write() method is rather frowned upon these days. However, with judicious and limited use, it can still be quite effective in certain cases.
It's two major drawbacks are:
If it is executed after page load, it overwrites the page.
It encourages creating markup from strings, which is less portable and often less effective than markup created using the DOM.
For simple one-liners though, it can be OK. You can (though it is not recommended) even use it to write out a concatenation produced from iterating over an array of strings (as I believe you are proposing), as long as the write phase occurs while the page is being parsed, at the point of parse. Otherwise, the overwrite disadvantage will kick in.
The original question here seems to me to have two parts though:
How do I concatenate an array of strings?
How do I inject this concatenated string into my document?
There are at least a few answers to both questions. Here is a demo showing one way:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var ar = ['string1', 'string2', 'string3'];
onload = function(){
document.getElementById('targetArea').firstChild.nodeValue = ar.join(' ');
};
</script>
</head>
<body>
<div id="targetArea"> </div>
</body>
</html>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.