Intermediate : JavaScript Basics
To use JavaScript's you need to know how to differentiate a JavaScript from regular HTML code. How do you recognize a JavaScript? Simple, you need to look for the tags. All JavaScript code have 2 tags <SCRIPT> at the beginning and </SCRIPT> at the end.
The opening tag is usually like this: <SCRIPT language="JavaScript"> The language="JavaScript" command is there so the browser can recognize that the code that is following is in JavaScript and not another scripting language, ex.: Visual Basic Script. VBScript. |
<SCRIPT language="JavaScript"> |
There is no limit in the number of scripts that an HTML page can contain. Don’t forget to always close your tags before continuing. Functions inside the <HEAD> </HEAD> tags of your document are JavaScript functions (we’ll get to this in the next tutorials). We always put these functions between the <HEAD> </HEAD> tags, this way your functions are loaded before the page begins to display, and you won't see all kinds of JavaScript errors. |
<HEAD> |
Before we begin writing scripts, we need to do something first. Old browsers are unable to read the <SCRIPT> tags. Instead of executing the JavaScript, they will display just the text. To solve this problem, we have to trick the browser into ignoring the text between the <SCRIPT> tags. HTML comments will be used, the older browsers will ignore the text inside the comments, but a JavaScript capable browsers will go ahead and perform your script. Here is how to do it: |
<SCRIPT language="JavaScript"> |
What can a JavaScript Do?
- JavaScript gives HTML designers a programming tool
- HTML authors are normally not programmers but JavaScript is a scripting language with a very simple syntax!
- Almost anyone can put small "snippets" of code into their HTML pages
- JavaScript can put dynamic text into an HTML page
- A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page
- JavaScript can react to events
- A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element
- JavaScript can read and write HTML elements
- A JavaScript can read and change the content of an HTML element
- JavaScript can be used to validate data
- A JavaScript can be used to validate form data before it is submitted to a server this will save the server from extra processing
- JavaScript can be used to detect the visitor's browser
- A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser
- JavaScript can be used to create cookies
- A JavaScript can be used to store and retrieve information on the visitor's computer
