JavaScript and XHTML…


Uses of JavaScript in XHTML

JavaScript is a lightweight programming language that was developed to enable more interactivity on static web pages.

JavaScript can be used in an HTML document between the <script> tags however, when you validate the HTML on your webpage the javaScript content will be considered as character data. It is better to follow XHTML standards so that the javaScript is considered parsed character data which is then processed by the validator.

Referencing JavaScript

JavaScript can be embedded within a document or it can be external. When the JavaScript is embedded all script has to be in the <head>.

<script type="text/javascript">
</script>
 
 

When a script is an external file-

<script type=”text/javascript” src=”ExternalJavaS.js”></script>

If… Statements-

There are 3 types of If Statements, which are all code that is to be executed upon a conditional statement.

  1. If…

Only if the specified condition is true

  1. If…Else

Use this statement if you want to execute some code if the condition is true and another code if the condition is false

  1. If…Else…If

Use this statement if you want to select one of many blocks of code to be executed

Loops-


Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true. For example, you can write code that will produce output that sums mathematical equations. If the start of the loop was 1 and you wanted to write answers for startLoop + 12,358 it would give u the answers.


There are 2 types of loops-

  1. For

Runs a loop through a block of code as many times as it has been specified

  1. While

Runs code while a condition is true. This type of loop can lead to infinite loops until the statement is false.

Loops can be broken in 2 different ways:


Break: The Break statement stops the current while or for loop and continues executing the code that follows after the loop (if any).

Continue: A Continue statement terminates execution of the block of statements in a while or for loop and continues execution of the loop with the next iteration.

Functions-

Functions are a way of organising and controlling different sequences of code which work together with other functions, page elements and input from the user. A function typically contains a set of commands for a specific purpose which you want to run at a certain time.

Each function in a script is given a unique name. The example below is named “doSomething”, although it could be given any name you like.

function doSomething() {
 // The function code goes here
}

Parameters-
Parameters may or may not be used with a function. A function may be called without parameters: function(); or have parameters: function(parameter).
This allows code to be minimised if paramters are used correctly.


<script language=”JavaScript”>
function box1(){alert(”You just opened box #1″);}
function box2(){alert(”This is box #2″);}
</script>
<a href=”#” onClick=”box1()”>Box #1</a><br>
<a href=”#” onClick=”box2()”>Box #2</a>

could be minimised to:

<script language=”JavaScript”>
function box(whichText)
{alert(whichText);}
</script>
<a href=”#” onClick=”box(‘You just opened box #1′)”>


Return Values-


The return statement is used to specify the value that is returned from the function. It is referenced by:

return variable;

Leave a comment

Filed under Uncategorized

Leave a comment