LILLYROBOT.COM |
JavaScript allows you to create dynamic and interactive web pages. It is an object-based scripting language, which was developed by NetScape in 1995. Java and JavaScript are not relatated. The official name for JavaScript is "ECMAScript".
<script type="text/javascript">
Statement goes in here
</script>
In HTML5 you can omit the
type ="text/javascript"
There are three ways to add JavaScript code on a web page:
The script tag is placed inside the <head></head>. Have a look at the example below; the function called add is inside the<head></head>, which is executed when the button inside the <body></body> is clicked.
Displays
3+9=?
The JavaScript codes are embeded into the HTML content. The example below shows the <script></script> inside the
<body></body>.
Displays
An external file contains nothing but JavaScript codes. The document is saved with a file name followed by the extension .js. You can then call the JavaScript file on the HTML page inside the <head></head>. An external file is useful if you want to save time in writing repetative codes on several pages.
<head>
<script src="documentName.js">
</script>
</head>
It is good practice to write comments about your JavaScript code. The comments cannot be seen on the browser. It helps you understand when the code was written, who wrote the code and what the code does.
The two types of comments are: