document.write() function

One of my online students was baffled moving from HTML coding to javascript. This is not surprising since they are very different languages. For my students, and others, that are struggling, the following tutorial may be helpful, http://w3schools.com/js/js_intro.asp.

I don’t consider myself a coder. Others are far more qualified. Still I’ll take a stab at answering specific question you may have. For students, post the question in the discussion forum. For visitors post the question in my contact form.

For this article, let me see if I can address the things this student has questioned.

Q:”I typed the code in as shown, but I didn’t comprehend how it worked.”

A: Did the code work? Some of the authors examples are Explorer and PC centric. For example the status bar code does not work in Firefox. So if your code doesn’t work, I can give you an alternate approach to illustrate the concept of an event handler.

Q: What are the plus signs for in the script on J-15?

A: The “+” sign can perform different functions depending on the context. In an arithmetic operation, it performs addition. When it is between “strings”, i.e text, it concatenates the strings together. For example, document.write(“I love” + ” javascript”), prints the two strings “I love” and “javascript” together so it looks like “I love javascript” on the screen. It is often used to mix strings and variables together. You can find out more at the following URL http://w3schools.com/js/js_operators.asp

Q: Why are there parentheses, then quotes, then quotes within quotes?

A: On page J-15 in your text, there is code in Figures J-14, 15, and 16. In figure J-16 is the following code:

document.write(“<h3>” + name + “, to subscribe to our newsletter, fill out the form below. </h3>”)

document.write() is a function. It prints what ever is in the parenthesis to the screen. The code wants the following to print to the screen. The name of the person  followed by the text “to subscribe…”. The name information is retrieved by the variable assignment in Figure J-14, ie.

var name=prompt(“Type your first name”)

The person is prompted to enter their first name. That information is saved in the variable “name”. Then the document.write() function prints the contents of the variable, the following text, and surrounds it all with an <h3> tag. So it prints the <h3> tag, then adds the variable value, then the text followed by the ending </h3> tag.

Let me know if you found this little tutorial helpful. Drop me a line.

Published by

Dale

eLearning and Instructional Designer. Web Designer. Assistant Professor for the Multimedia Design Concentration at Nashville State Community College. Eleven years with Nashville State. Seven years as a corporate trainer, teaching CAD, design, and office applications. Developed Associate in Applied Science degree program in Multimedia Design and teach web-standards, Flash, html, CSS, web animation, and design courses. Teach in both classroom and online environments. Education: M.Ed. in eLearning Technology and Design B.S. in Mechanical Drafting and Design Technology