Conditional statements are used
to perform different actions based on different conditions.
The JavaScript Switch Statement
Use the switch statement to select
one of many blocks of code to be executed.
Syntax
switch(n)
{ case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 } |
Example
|

JavaScript Popup Boxes
JavaScript has three kind of
popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you
want to make sure information comes through to the user.When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
alert("sometext");
|
Example
|
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
confirm("sometext");
|
Example
|
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
prompt("sometext","defaultvalue");
|
Example
|
JavaScript Functions
A function will be executed by an
event or by a call to the function.
JavaScript Functions
To keep the browser from executing
a script when the page loads, you can put your script into a function.A function contains code that will be executed by an event or by a call to the function.
You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file).
Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.
How to Define a Function
Syntax
function functionname(var1,var2,...,varX)
{ some code } |
Note: A function with no parameters must include the parentheses () after the function name.
Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.
JavaScript Function Example
Example
|
You will learn more about JavaScript events in the JS Events chapter.
The return Statement
The return statement is used to
specify the value that is returned from the function.So, functions that are going to return a value must use the return statement.
The example below returns the product of two numbers (a and b):
Example
|
The Lifetime of JavaScript Variables
If you declare a variable within a
function, the variable can only be accessed within that function. When you exit
the function, the variable is destroyed. These variables are called local
variables. You can have local variables with the same name in different
functions, because each is recognized only by the function in which it is
declared.If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
JavaScript For Loop
Loops execute a block of code a
specified number of times, or while a specified condition is true.
JavaScript Loops
Often when you write code, you
want the same block of code to run over and over again in a row. Instead of
adding several almost equal lines in a script we can use loops to perform a
task like this.In JavaScript, there are two different kind of loops:
- for - loops through
a block of code a specified number of times
- while - loops through
a block of code while a specified condition is true
The for Loop
The for loop is used when you know
in advance how many times the script should run.
Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{ code to be executed } |
Example
The example below defines a loop
that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs.Note: The increment parameter could also be negative, and the <= could be any comparing statement.
Example
|
The while loop
The while loop will be explained
in the next chapter.
JavaScript While Loop
Loops execute a block of code a
specified number of times, or while a specified condition is true.
The while Loop
The while loop loops through a
block of code while a specified condition is true.
Syntax
while (var<=endvalue)
{ code to be executed } |
Example
The example below defines a loop
that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs:
Example
|
The do...while Loop
The do...while loop is a variant
of the while loop. This loop will execute the block of code ONCE, and then it
will repeat the loop as long as the specified condition is true.
Syntax
do
{ code to be executed } while (var<=endvalue); |
Example
The example below uses a
do...while loop. The do...while loop will always be executed at least once,
even if the condition is false, because the statements are executed before the
condition is tested:
Example
|
The break Statement
The break statement will break the
loop and continue executing the code that follows after the loop (if any).
Example
|
The continue Statement
The continue statement will break
the current loop and continue with the next value.
Example
|
JavaScript Animation
With JavaScript we can create
animated images.
JavaScript Animation
It is possible to use JavaScript
to create animated images.The trick is to let a JavaScript change between different images on different events.
In the following example we will add an image that should act as a link button on a web page. We will then add an onMouseOver event and an onMouseOut event that will run two JavaScript functions that will change between the images.
The HTML Code
The HTML code looks like this:
<a href="http://www.w3schools.com"
target="_blank">
<img border="0" alt="Visit W3Schools!" src="b_pink.gif" id="b1" onmouseOver="mouseOver()" onmouseOut="mouseOut()" /></a> |
The onMouseOver event tells the browser that once a mouse is rolled over the image, the browser should execute a function that will replace the image with another image.
The onMouseOut event tells the browser that once a mouse is rolled away from the image, another JavaScript function should be executed. This function will insert the original image again.
The JavaScript Code
The changing between the images is
done with the following JavaScript:
<script type="text/javascript">
function mouseOver() { document.getElementById("b1").src ="b_blue.gif"; } function mouseOut() { document.getElementById("b1").src ="b_pink.gif"; } </script> |
The function mouseOut() causes the image to shift to "b_pink.gif".
https://www.welookups.com/js/js_objects.html
ReplyDelete