Loading ...
Sorry, an error occurred while loading the content.

Re: document.write messages

Expand Messages
  • aceblchboy
    ... document.write can be a form of eval. From Crockford s JSlint instructions: eval is evil The eval function (and its relatives, Function, setTimeout, and
    Message 1 of 4 , Jul 10 9:26 PM
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "stevengrindle2002" <stevengrindle2002@...> wrote:
      >
      > Folks,
      >
      > I am new to JSLint but have used JavaScript in several web pages I have created. I am working my way through the JSLint error messages, correcting my code, learning how to set the options and trying to make my JavaScript code better.
      >
      > 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.
      >
      document.write can be a form of eval.
      From Crockford's JSlint instructions:
      eval is evil
      The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. The eval function is the most misused feature of JavaScript.

      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"));
    • Marc Draco
      document.write() is evil because it affects the document as it s been rendered. There are some (very,very few) instances when you might need to do this. I have
      Message 2 of 4 , Jul 11 5:34 AM
      • 0 Attachment
        document.write() is evil because it affects the document as it's been
        rendered. There are some (very,very few) instances when you might need
        to do this.

        I have used it recently to change a form on the client side depending on
        whether or not Javascript was active, like so:

        <script type="text/javascript">
        document.writeln("<input id='contactFormSend' disabled='disabled'
        value='Send Request' type='submit' />");
        </script>

        <noscript>
        <input value="Send Request" type="submit" />
        </noscript>

        This isn't something that can easily be done on the server and allows
        the receiving code to see if Javascript was active when the form was
        sent - at least, when the page was loaded. The server uses this to
        determine the likelihood that the form has had minimal validation or
        none at all and warn the user.

        Using document.write or document.writeln as a debugging measure is
        asking for trouble. Until recently (hang my head in shame) I relied on
        Alert() or some concoction of DIVs. <shudders>

        A far better solution is Firefox with the Firebug extension. Firebug can
        write to a special console area:

        console.log(var1, var2, object, ...);

        and is very, very good at it too. You can even examine events objects
        (for example) just by passing them like this:

        function eventHandler(e)
        {
        console.log(e);
        //...
        }

        Firebug allows you to decompose the entire event, method by property in
        the console window which can be very handy indeed.
      • Cheney, Edward A SSG RES USAR USARC
        document.write is essential when executing JavaScript where a DOM is not available, such as all possible methods of interpretation outside for a webpage in a
        Message 3 of 4 , Jul 11 7:14 PM
        • 0 Attachment
          document.write is essential when executing JavaScript where a DOM is not available, such as all possible methods of interpretation outside for a webpage in a conforming user-agent. Other than those conditions I don't see why this method should ever be used.

          Austin Cheney
          http://prettydiff.com/
        Your message has been successfully submitted and would be delivered to recipients shortly.