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:
Code:
<!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>
Bookmarks