Results 1 to 3 of 3

Thread: [jshint] "document.write can be a form of eval."

  1. #1
    Join Date
    Jun 2009

    Question [jshint] "document.write can be a form of eval."


    I'm learning JavaScript by reading "The Definitive Guide".

    I saw JSHint and JSLint recommended to catch syntax errors, but I don't understand why they don't like this code:

    var s = "hello world!"; // A string
    var word = s.substring(s.indexOf(" ") + 1, s.length); // Use string properties
    Here's the report:
    Line 3: document.write(word);
    document.write can be a form of eval.
    Why is document.write() wrong, and what else should I use to simply output something to the browser?

    Thank you.

  2. #2
    Join Date
    Nov 2010
    You can google for why it is wrong - much has been written on the subject. The simplest way to output is by giving an element like a div an ID and doing document.getElementById("theid").innerHTML=word;

    If you are using a modern browser you can also log to the console by doing console.log(word);

    Further along the line you may want to explore createElement(), but that is more complicated and results are very ambiguous as to whether it is better.

  3. #3
    Join Date
    Jun 2009
    Thanks for the infos. I'll use getElementById().

    As for not using document.write(), I found this: "Why is document.write considered a 'bad practice'?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center