JavaScript Introduction – Writing JavaScript with HTML

JavaScript Introduction – Writing JavaScript with HTML cover image
  1. Home
  2. JavaScript
  3. JavaScript Introduction – Writing JavaScript with HTML

Take a look at the following code:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

document.write("JavaScript is <B>great</B>.");

//-->
</SCRIPT>

You’ll notice that along with the text I’ve thrown in the HTML bold tag. HTML tags enclosed in write() are not displayed. The browser interprets these tags accordingly and then prints the result. In our case, the word ‘great’ is displayed in bold.
Click here to view the result.

Here is a code that prints text in blue color:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--

document.write('<FONT COLOR="#0000FF">Blue denim</FONT>');

//-->
</SCRIPT>

Click here to view the result.

Note that I have used single quotes to enclose the text in write(). Why? Because it is my habit to contain HTML attribute values with double quotes. If I had used double quotes to surround both the attribute value and the text in write(), I would have received an error from the JavaScript interpreter.
This kind of quote nesting is used often with event handlers and it’s important that you remember it.

JavaScript allows inclusion of all HTML tags in write(). You can actually build entire HTML documents starting from <HTML> tag!

JavaScript