Teachers Note - Section 3
Teachers Note - Section 3
Level 1 - Semester 2
<SCRIPT LANGUAGE=“JavaScript”>
JavaScript statements here
</SCRIPT>
© 2020 e-Learning Centre, UCSC
13
Embedding JavaScript in XHTML
• <script> tag is used to embed JavaScript code in
XHTML code of a web page
• The <script> tag can be used anywhere inside the
code, but it is usually embedded right before of after
the <head> tag closes
• Any number of <script> tags can be embedded, but
usually one tag is enough
• Nesting <script> tags is prohibited and generates
errors
• HTML editors do not follow the <script> tag
guidelines and embed the tag any where and any
number of times
© 2020 e-Learning Centre, UCSC 14
Development Environment
• JavaScript source code is written in an editor and run
and tested in a browser, like XHTML
• Error in JavaScript code prevent the page from being
rendered and thus debuggers are needed to find where
the errors are
• JavaScript interpreters serve the purpose by showing
where the error is
• Errors are reported one at a time and are usually easy to
fix
document.write('This is my first →
JavaScript Page');
Note the symbol for
</script> line continuation
</body>
</html>
document.write('<h1>This is my first →
JavaScript Page</h1>');
HTML written
</script> inside JavaScript
</body>
</html>
You can also declare multiple variables with the same var keyword as follows −
For instance, you might create a variable named money and assign the value 2000.50 to it later. For
another variable, you can assign a value at the time of initialization as follows.
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data
type. Unlike many other languages, you don't have to tell JavaScript during variable declaration what
type of value the variable will hold. The value type of a variable can change during the execution of a
program and JavaScript takes care of it automatically.
© 2020 e-Learning Centre, UCSC 23
Variables…
JavaScript variables have only two scopes.
• Global Variables − A global variable has global scope which means it can be
defined anywhere in your JavaScript code.
• Local Variables − A local variable will be visible only within a function where it is
defined. Function parameters are always local to that function.
<html>
<body onload = checkscope();>
<script type = "text/javascript">
<!--
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar);
}
//-->
</script>
</body>
</html>
24
© 2020 e-Learning Centre, UCSC
Variables
• A variable can hold data such as numbers or characters
• A variable name must with a letter,
• an underscore(“_”)
• or a dollar($)
<body>
<script language="javascript">
<!--
a=100;
document.write(a);
abc=20-10;
document.write(abc);
_abc=30-5;
document.write(_abc);
$abc=40-2;
document.write($abc);
answer=100-10*2;
document.write(answer);
//-->
</script>
</body> 25
Variables
• Scope
The code block within which the variable is available
Global variable is available everywhere
Local variable is available only inside a code block
Global variables are easy to manage but a bad habit
• Constants
Read only variables defined by a const keyword
Cannot change values or be re declared
Examples: const x=22
• Literals
Fixed values (hard-coded values) in JavaScript
Nesting literals needs extra care
Examples: 3.5, false, “Hello”
• Data Type Conversion
JavaScript converts data types automatically, but creates confusion
Examples: answer=true, answer=20
• Escaping and Special Characters
Backslash is the escaping character and is used to define special ones
© 2020 e-Learning Centre, UCSC 26
Statements
• A statement uses an assignment operator, an equal sign
• The operator has two operands on each of its side and the value of
the right operand is assigned to the left one
Example : x = y
TRUE
Process
Increment
© 2020 e-Learning Centre, UCSC
32
Conditional Branching
• Use the if statement to perform separate statements according to a condition
if
FALSE
if (condition){ Condition
statement for when condition1 is true;
} else { TRUE
statement for when condition1 false Process1 Process2
}
Else if
if (condition1){ FALSE
statement for when condition1 is true; Condition1
} else if (condition2){
statement for when condition2 true; TRUE
FALSE
} else { Process1 Condition2
statements for when all condition are false;
} TRUE
Process2 Process3
<form name="addressform">
Name: <input name="yourname"><br />
Phone: <input name="phone"><br />
Email: <input name="email"><br />
</form>
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
46
© 2020 e-Learning Centre, UCSC
Using Form Data
Personalising an alert box
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' + →
document.alertform.yourname.value);">
</form>
:
:
//-->
</script> </head><body>
Please fill up these text boxes(all inputs are required).<br>
<form action ="flm.cgi" name="fm" onSubmit="return checkForm()">
Postal Code:
<input type="text" Name="yubin" size="8"><br>
Address:
<input type="text" Name="address" size="40"><br>
Name:
<input type="text" Name="name" size="20"><br>
<input type="submit" value="Submit">