0

I am presently studying javascript coding practice on CodeAcademy. When testing code in Codeacademy I use console.log to output strings to the built in browser within Codeacademy. The code works fine. The first issue is this: When I test the same piece of code within Dreamweaver text editor and output it to the browser it prints nothing, I have to change it to document.write for it to work.

Next Part I then read somewhere that using document.write in production code is not recommended! Can somebody explain this.

Next Part I was at a brief introduction to JS free meeting a few days ago. At this meeting it was suggested that using something like prompt("La di da"); is not recommended in production work.

If anybody has the time and energy to explain why these things are built into JS but no recommended to be used or why they do not work when used, I would be very grateful.

  • 4
    because console.log() prints to your console not on browser document..! – Sudhir Bastakoti Nov 27 '13 at 11:09
  • for document.write check this:: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Sudhir Bastakoti Nov 27 '13 at 11:11
  • We can't tell you why some code we can't see isn't working. Recommendations to avoid document.write and prompt altogether are overly broad and shouldn't be taken too seriously. They have their places. – Quentin Nov 27 '13 at 11:11
  • 1
    The downvote is a little unfair... These are good questions for a starter to ask (although perhaps best asked/researched as separate questions). – Josh Harrison Nov 27 '13 at 11:17

1 Answers1

2
  1. Code Academy will be emulating a console in their web application. Press "F12" in most browsers, and you'll get "Developer Tools" opening up; which will have a console built in, which is where console.* (including console.log()) calls get output to.

    Like I said, Code Academy will have some of their own JavaScript, which will be catching these calls, and to make their tutorials easier to use, outputting it to a place which is easier for you to see.

    Dreamweaver however, won't be doing this, which is why you're not seeing it.

  2. There is nothing inherantly wrong with using document.write. However it behaves differently depending on whether the page has loaded or not, and there are generally friendlier and more useful alternatives such as document.getElementById() for targeting where to direct output.

    For more info, see Why is document.write considered a "bad practice"?

  3. As with document.write, there is nothing wrong with prompt(), confirm() etc; Stack Overflow themselves use confirm() on their websites. The downside is that they cannot be styled, and prompt() for example, is restricted to asking for one thing at a time.

    Model windows however (such as jQuery UI dialog, Bootstraps "Model" or various lightbox plugins, can be.

Community
  • 1
  • 1
Matt
  • 70,063
  • 26
  • 142
  • 172