1364Re: document.write messages
- Jul 10, 2010--- In email@example.com, "stevengrindle2002" <stevengrindle2002@...> wrote:
>document.write can be a form of eval.
> I get lot of messages about 'document.write' because I use it frequently. I would like to understand
> 1) Why does JSLint object to 'document.write'?
> 2) What should I do to fix my code?
> Example 1 of dozens of similar messages:
> Problem at line 15 character 5: document.write can be a form of eval.
> document.write(nbr+" is nbr<br />"); //absolute value
> Steven G.
From Crockford's JSlint instructions:
eval is evil
The eval method takes a string containing JScript code, compiles it and runs it, but the computing cost-effectiveness of starting an interpreter to compile it outweighs other far more lightweight methods that achieve exactly what you want.
Try obtaining a reference to the desired element where you want to place your text or HTML, and drop your code in with the easy non-standard way: elementReference.innerHTML = nbr+" is nbr<br />" or the standards way elementReference.appendTextNode(nbr+" is nbr").appendChild(document.createElement("br"));
- << Previous post in topic Next post in topic >>