Basic JavaScript
Loi Ngoc Nguyen, Duy-Ky Nguyen, PhDWhat is JavaScript?
- JavaScript was designed to add interactivity to HTML pages
- JavaScript is a scripting language
- A scripting language is a lightweight programming language
- A JavaScript consists of lines of executable computer code
- A JavaScript is usually embedded directly into HTML pages
- JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
- Everyone can use JavaScript without purchasing a license
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 saves 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
Comment is
// Single-line comment . . . */ |
Example 1
<html> |
The code above will produce this output on an HTML page:
Hello World! |
Where to Put the JavaScript
JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.
Scripts in the head section: Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it.
<html> |
Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.
<html> |
Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
<html> |
Using an External JavaScript
Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page.
To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the <script> tag!
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
<html> |
Note: Remember to place the script exactly where you normally would write the script!
JavaScript Events
By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.
Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.
Examples of events:
- A mouse click
- A web page or an image loading
- Mousing over a hot spot on the web page
- Selecting an input box in an HTML form
- Submitting an HTML form
- A keystroke
Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!
Event Handlers
New to HTML 4.0 was the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. Below is a list of the attributes that can be inserted into HTML tags to define event actions.
FF: Firefox, N: Netscape, IE: Internet Explorer
Attribute | The event occurs when... | FF | N | IE |
---|---|---|---|---|
onabort | Loading of an image is interrupted | 1 | 3 | 4 |
onblur | An element loses focus | 1 | 2 | 3 |
onchange | The user changes the content of a field | 1 | 2 | 3 |
onclick | Mouse clicks an object | 1 | 2 | 3 |
ondblclick | Mouse double-clicks an object | 1 | 4 | 4 |
onerror | An error occurs when loading a document or an image | 1 | 3 | 4 |
onfocus | An element gets focus | 1 | 2 | 3 |
onkeydown | A keyboard key is pressed | 1 | 4 | 3 |
onkeypress | A keyboard key is pressed or held down | 1 | 4 | 3 |
onkeyup | A keyboard key is released | 1 | 4 | 3 |
onload | A page or an image is finished loading | 1 | 2 | 3 |
onmousedown | A mouse button is pressed | 1 | 4 | 4 |
onmousemove | The mouse is moved | 1 | 6 | 3 |
onmouseout | The mouse is moved off an element | 1 | 4 | 4 |
onmouseover | The mouse is moved over an element | 1 | 2 | 3 |
onmouseup | A mouse button is released | 1 | 4 | 4 |
onreset | The reset button is clicked | 1 | 3 | 4 |
onresize | A window or frame is resized | 1 | 4 | 4 |
onselect | Text is selected | 1 | 2 | 3 |
onsubmit | The submit button is clicked | 1 | 2 | 3 |
onunload | The user exits the page | 1 | 2 | 3 |
Example 1
<h1 onclick="this.innerHTML='Ooops!'">Click on this text</h1>
|
and the output
Ooops! |
Example 2
<h1 id="header1" onclick="this.style.color='red'">Click Me!</h1> <p>If you click the header above, it turns red.</p> |
and the output
Click Me!If you click the header above, it turns red. |
Example 3
<h1 onclick="document.getElementById('p1').style.color='red'">Click Me!</h1> |
and the output
Click Me!If you click the header above, I turn red. |
Example 4
<h1 onmouseover="style.color='red'" |
and the output
Mouse over this text |
Example 5
<p id="p2"> Hello World! </p> |
and the output
Hello World! |
Example 6
Execute upon completion of input
<script type="text/javascript"> |
and the output
Enter your name: |
Example 7
Execute upon mouse click
Field1: <input type="text" id="field1" value="Hello World!"><br /> |
and the output
Field1: Field2: Click the button below to copy the content of Field1 to Field2. |
Example 8
Move mouse in & out the object
<head> |
Move mouse in and out the object |
Example 9
Turn On/Off the light
<head> |
and the output
Click to turn on/off the light |
Example 10
Convert to upper case upon completion of input
Turn On/Off the light
<head> |
and the output
Press to turn on the light |