CN0122 Lect9
CN0122 Lect9
- NOTE:
- Old JavaScript examples may use a type attribute: <script type="text/javascript">.
- The type attribute is not required. JavaScript is the default scripting language in HTML.
JavaScript - Where To
- Most preferred way to include JavaScript in an HTML file are:
1. Script in <head>...</head> section.
2. Script in <body>...</body> section.
3. Script in <body>...</body> and <head>...</head> sections.
4. Script in an external file and then include link in <head>...</head>
section.
1. JavaScript: In <head>...</head> section
- If you want to have a script run on some event, such as when a user clicks
somewhere, then you will place that script in the head as follows:
2. JavaScript: In <body>...</body> section
- If you need a script to run as the page loads so that the script generates content in the page, then
the script goes in the <body> portion of the document.
- In this case, you would not have any function defined using JavaScript.
- Take a look at the following code.
- Example:
3. JavaScript: In <body> and </head> section
- You can put your JavaScript code in <head> and <body> section altogether as follows:
- Example:
4. JavaScript: In External File
- Useful in cases where you are reusing identical JavaScript code on multiple pages of a site
- The script tag provides a mechanism to allow you to store JavaScript in an external file and then
include it into your HTML files
- This involves the use of the src attribute as seen in the example below:
JavaScript: Variables
- 5 +1 = 6
- 5-1=4
- 5 * 2 = 10
- 4/2 = 2
- 5%2 = 1
- i =5, i++ = 6
- i =5, i-- = 4
JavaScript: Assignment Operators
- Assignment operators assign values to JavaScript variables.
JavaScript: Adding Strings and Numbers
- The + operator can also be used to add (concatenate) strings.
JavaScript: String Operators
- Adding two numbers, will return the sum, but adding a number and a string
will return a string.
JavaScript: Comparison Operators
- “== ” - equal to
- “!=” - not equal to
JavaScript: Conditions
- Conditional statements are used to perform different actions based on
different conditions.
- Very often when you write code, you want to perform different actions for
different decisions.
- You can use conditional statements in your code to do this.
- In JavaScript we have the following conditional statements:
● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed, if the same condition is false OR
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed
JavaScript: Conditions ..The if statement
- See Example:
JavaScript: Conditions ..The else statement
- Use the else statement to specify a block of code to be executed if the condition is false.
- See Example:
JavaScript: Conditions ..The else if statement
- Use the else if statement to specify a new condition if the first condition is false.
- See Example:
JavaScript: Statements
- You can reuse code: Define the code once, and use it many times.
- You can use the same code many times with different arguments, to produce
different results.
JavaScript: More on Functions
- The () Operator Invokes the Function
- Using the example above, toCelsius refers to the function object, and toCelsius() refers to the
function result.
- Functions Used as Variable Values
- Functions can be used the same way as you use variables, in all types of formulas,
assignments, and calculations.
JavaScript: Events
- HTML events are "things" that happen to HTML elements.
- When JavaScript is used in HTML pages, JavaScript can "react" on these
events.
- An HTML event can be something the browser does, or something a user
does.
- Examples:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
- Often, when events happen, you may want to do something.
- JavaScript lets you execute code when events are detected.
JavaScript: Events Cont...
- HTML allows event handler attributes with JavaScript code to be added to
HTML elements.
- Example:
JavaScript: Events Cont...
- JavaScript code is often several lines long. It is more common to see event
attributes calling functions:
JavaScript: Events Cont...
- More Events:
JavaScript: Loops
syntax
● window.alert("sometext");
Confirm Box
● A confirm box is often used if you want the user to verify or accept
something.
● When a confirm box pops up, the user will have to click either "OK" or
"Cancel" to proceed.
● If the user clicks "OK", the box returns true. If the user clicks
"Cancel", the box returns false.
Syntax
window.confirm("sometext");
● The window.confirm() method can be written without the window
prefix.
Prompt Box
● A prompt box is often used if you want the user to input a value before
entering a page.
● When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value.
● If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.
Syntax
● window.prompt("sometext","defaultText");
Window Location Href
;
Example
● Display the href (URL) of the current page:
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
END
END