397Chapter 18 .The Document and Body Objects document.write() (Free web space)

397Chapter 18 .The Document and Body Objects document.write() statement. Also be sure to include a document.close()statement in the next line of script. Assembling HTML in a script to be written via the document.write() method often requires skill in concatenating string values and nesting strings. A number of JavaScript String object shortcuts facilitate the formatting of text with HTML tags (see Chapter 34 for details). If you are writing to a different frame or window, you are free to use multiple document.write() statements if you like. Whether your script sends lots of small strings via multiple document.write() methods or assembles a larger string to be sent via one document.write() method depends partly on the situation and partly on your own scripting style. From a performance standpoint, a fairly standard procedure is to do more preliminary work in memory and place as few I/O (input/output) calls as possible. On the other hand, making a difficult-to-track mistake is easier in string concatenation when you assemble longer strings. My personal preference is to assemble longer strings, but you should use the system that s most comfortable for you. You may see another little-known way of passing parameters to these methods. Instead of concatenating string values with the plus (+) operator, you can also bring string values together by separating them with commas. For example, the following two statements produce the same results: document.write( Today is + new Date()) document.write( Today is ,new Date()) Neither form is better than the other, so use the one that feels more comfortable to your existing programming style. Note Dynamically generating scripts requires an extra trick, especially in NN. The root of the problem is that if you try code, such as document. write( ), the browser interprets the end script tag as the end of the script that is doing the writing. You have to trick the browser by separating the end tag into a couple of components. Escaping the forward slash also helps. For example, if you want to load a different .js file for each class of browser, the code looks similar to the following: // variable browserVer is a browser-specific string and // page is the HTML your script is accumulating for document.write() page +=