JavaScript and HTML Text

JavaScript is a client-side scripting language that runs entirely inside the web browser. To call it up, you place it between opening HTML tags.
A typical HTML 4.01 “Hello World” document using JavaScript might look like following Example.

<head><title>Hello World</title></head>
<script type="text/javascript">
document.write("Hello World")
Your browser doesn't support or has disabled JavaScript

Within the script tags is a single line of JavaScript code that uses its equivalent of the PHP echo or print commands, document.write. As you’d expect, it simply outputs the supplied string to the current document, where it is displayed.
You may also have noticed that, unlike PHP, there is no trailing semicolon (;). This is because a new line acts the same way as a semicolon in JavaScript. However, if you wish to have more than one statement on a single line, you do need to place a semicolon after each command except the last one. Of course, if you wish, you can add a semicolon to the end of every statement and your JavaScript will work fine.
The other thing to note in this example is the pair of tags. These are used when you wish to offer alternative HTML to users whose browser does not support JavaScript or who have it disabled. The use of these tags is up to you, as they are not required, but you really ought to use them, because it’s usually not that difficult to provide static HTML alternatives to the operations you provide using Java-Script. However the remaining examples in these tutorials will omit <noscript> tags, because we’re focusing on what you can do with JavaScript, not what you can do without it.


You may also like...