Scripting to a Webpage

HTML file: Displayed by browser:
<!DOCTYPE html>
<HTML>
<BODY>

<SCRIPT LANGUAGE="javascript">
document.write("<FONT COLOR='RED'>This Is Red Text</FONT>")
</SCRIPT>

</BODY>
</HTML>

Deconstructing the Script:

<script language="javascript">
This simply alerts the browser that what immediately follows is a JavaScript script. Now the language attribute has been deprecated for a long time, and does not need to be used. With HTML5, all browsers have "text/javascript" as the default script type so it's considered standard default value. However, for pages in XHTML 1.0 or HTML 4.01, omitting the language type is considered invalid, causing an error which says that "required atribute 'type' is not specified". Note that there are other types of scripts (Applescript, Perl, PHP, Unix Shell, VBScript), so even if it may have no practical effect, it won't hurt to type it in, if I am a fan of standards.

document.write
The document holds the contents of the page within the browser window, including all HTML codes and JavaScript commands. I think of it as the HTML document. This document is altered by write-ing what follows inside the parentheses.

To use tech-speak, the document is an object. The write that follows, separated by a period, is the object's method (an action to be performed on the object.) So the script is saying: take the object (something that already exists) and write something to it.

Double Parentheses
The double parentheses are called the instance. The text inside the instance is the method's parameters, or what will be done when the method is acted upon the object. It could even hold some HTML flags which is designed to enhance the results.

Quotation Marks
The contents inside the parentheses are enclosed in double quotation marks. In HTML, quotation marks are not always required, but in JavaScript, they are. I must use them and there is an exact way to use them.

Notice the single quotation around RED. If I used double quotations, JavaScript assumes the second set marks the end of the line and I'd only get part of the text written to the object. I don't have to use quotes around HTML attributes, but if I do, I just need to remember: use single quotes inside double quotes.

Be careful not to use words that are contracted like "can't", "don't" or "I'm". JavaScript will assume that I've started an attribute with that single quotation mark and an error will result, ensuring that the script will not work.
BackTable of ContentsNext