-
Devilish(?) document.write
The script used here to create JS out of HTML/JS/CSS (see source) relies on document.write. It's a great script. Extensive testing (I tested the most complicated external HTML/JS/CSS) has convinced me that the include-script written by Joseph K. Myers is very useful for those who cannot use server side includes and who don't want to use Ajax nor an iframe or a text/html-object to include external content.
Now, it is often said that document.write is evil and that, therefore, we should avoid it. There may be a misunderstanding here. We should distinguish indeed between evil use of document.write and document.write itself. The brute force of document.write allows us, for instance, to include whole external files into a document, together with all the sections of the external files: html-section, head-section, body-section, meaning that we may end up with more than one html-section, head-section etc. in a document. Note however that, if that would be the result of applying document.write to a document, the blame would not be on document.write itself, but on the way we used it.
Another type of criticism on document.write concerns the fact that it can be used, and is actually often used, as storing code (css, js) into strings. Here again, the criticism pertains to the way document.write is (can be) used, not to document.write itself: if you don't want to use document.write for representing code, then don't. As a matter of fact, the requirement according to which document.write should only be used for storing pure data, not for storing code, is not justified by what common practice shows us in PHP functions and in JSON. If you know what you are doing, storing code in strings will not result in something that is necessarily bad. (The question whether representing code as a string is 'bad style' is another matter, of course. Anyhow, bad style that is not insecure is not really bad, IMHO).
I don't say that there are no cases in which document.write causes problems (although I didn't encounter them yet on my own test pages). But if you apply it with wisdom, there shouldn't be a problem. Note also that document.write is very fast and allows us to 'automatically' import virtually anything. (Automatically (!) importing JS and CSS with the help of valid DOM-methods is very hard to accomplish).
So what do you think?
===
Arie.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks