html body { margin-top: 50px !important; } #top_form { position: fixed; top:0; left:0; width: 100%; margin:0; z-index: 2100000000; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -o-user-select: none; border-bottom:1px solid #151515; background:#FFC8C8; height:45px; line-height:45px; } #top_form input[name=url] { width: 550px; height: 20px; padding: 5px; font: 13px "Helvetica Neue",Helvetica,Arial,sans-serif; border: 0px none; background: none repeat scroll 0% 0% #FFF; }
Chapter 6 - JAVASCRIPT - Hamid
Chapter 6 - JAVASCRIPT - Hamid
Scripting
Chapter 6
Objectives
1 What is JavaScript
2 JavaScript Design
3 Using
JavaScript 4 Syntax
5 JavaScript
Objects
Section 1 of 5
What IS JavaScript
Three layers of web design
JAVASCRIPT!
Client-Side Scripting
Let the client compute
scripts
• These are program codes
written inside HTML page.
JScript:
– Microsoft version of JavaScript, introduced in 1996
– same core language, but some browser-specific
differences
fortunately, IE, Netscape, Firefox, etc. can (mostly)
handle both JavaScript & JScript
JavaScript 1.5 & JScript 5.0 cores both conform to
ECMAScript standard
VBScript:
– client-side scripting version of Microsoft Visual Basic
What is JavaScript?
JavaScript was designed to add interactivity to HTML pages.
• Inline
• Embedded
• External
JavaScript
• JavaScript consists of JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
• The <script> tag alert the browser program to begin interpreting all the text
between these tags as a script.
• The script tag takes two important attributes:
language:
• This attribute specifies what scripting language you are using.
• Typically, its value will be “javascript”.
type:
• This attribute is what is now recommended to indicate the scripting language
in use. its value should be set to "text/javascript".
script commands
</script>
</body>
</html>
How to Put a JavaScript Into an HTML
Page 2?
<html>
<Head>
<script type=“ text/javascript ">
script commands
</script>
<Head>
<body>
</body>
</html>
How to Put a JavaScript Into an HTML
Page 3?
<html>
<Head>
<body>
</body>
</html>
Using External JavaScript (contd.)
JavaScript.html
Embedding JavaScript
External Javascript File
<body>
...
<script type="text/javascript" src="myCode.js" />
<script type="text/javascript">
//<![CDATA[
alert("Page is loading"); Inline Code
//]]>
</script>
Example:
<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another
paragraph.</p>");
</script>
JavaScript Statements
JavaScript Blocks
JavaScript statements can be grouped together in blocks.
Blocks start with a left curly bracket { and end with a right curly
bracket }.
The purpose of a block is to make the sequence of statements
execute together.
This example will write a heading and two paragraphs to a web
page:
<script type="text/javascript">
alert("This is an Alert method");
confirm("Are you OK?");
prompt("What is your name?");
prompt("How old are you?","20");
</script>
alert() and confirm()
alert("Text to be displayed");
If the user click the "OK" button, prompt() returns the value in
the input textfield as a string.
If the user click the "Cancel" button, prompt() returns null.
JavaScript Comments
Comments can be added to explain the JavaScript, or to make the code more
readable.
Single line comments start with //.
Multi line comments start with /* and end with */.
<html>
<body>
<script type="text/javascript">
// This is Comment 1..
/*
This is Comment 2..
this is multi-line comment
*/
</script>
</body>
</html>
Section 4 of 8
A variable can have a short name, like x, or a more descriptive name, like
carname.
36
JavaScript Varibles
Declaring (Creating) JavaScript Variables
Creating variables in JavaScript is most often referred to as "declaring" variables.
You declare JavaScript variables with the var keyword:
– var x;
var carname;
After the declaration shown above, the variables are empty (they have no values
yet).
However, you can also assign values to the variables when you declare them:
– var x=5; // will hold the value 5,
var carname="Volvo"; // and carname will hold the value Volvo
Note: When you assign a text value to a variable, use quotes around the value.
Note: If you redeclare a JavaScript variable, it will not lose its value.
37
Data Type in JavaScript (contd.)
JavaScript recognizes the following type of values:
Values
numbers boolean
string 9, 3.56 true or false
null
Samuel, ”Samuel J Palmisano”
A Special keyword which refers to nothing
JavaScript Data type
JavaScript variables can hold many data types:
numbers, strings, arrays, objects and more:
<html>
<head>
</head>
<body>
<script type="text/javascript">
txt3=txt1+txt2;
document.write(txt3);
</script>
</body>
</html>
Prompts
prompt() method
– The prompt() method displays a dialog box with a message, a
text box, an OK button & a Cancel button.
– It requests a data from the user.
var myName;
myName = prompt(“prompt message”);
Using JavaScript
Hello Nora
Hands-on Practice : The Code
<body>
<h1>Using JavaScript</h1>
<h2>Your two favorite colours are:
<script type="text/javascript">
<!--
var favcolour, favcolour2;
favcolour = prompt("What is your Favorite colour?");
favcolour2 = prompt("How about your second favorite colour?");
document.write(favcolour," ", favcolour2);
// -->
</script>
</h2>
JavaScript Variables Scope
Local JavaScript Variables
A variable declared within a JavaScript function becomes LOCAL and can only be
accessed within that function. (the variable has local scope).
<script>
x=1
Global scope
var y=2
function MyFunction()
{
var z
Local scope
z=3
// the rest of the code
}
</script>
JavaScript Operators
Operators
String
Comparison
typeof
Logical
JavaScript Operators
JavaScript Arithmetic Operators
• Arithmetic operators are used to perform arithmetic between variables and/or
values.
• Given that y=5, the table below explains the arithmetic operators:
49
JavaScript Operator (contd.)
JavaScript String Operators
JavaScript Operators
The + Operator Used on Strings
The + operator can also be used to add string variables or text values
together.
After the execution of the statements above, the variable txt3 contains
"What a verynice day".
51
JavaScript Operators
The + Operator Used on Strings
To add a space between the two strings, insert a space into one of the
strings:
txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;
or insert a space into the expression:
txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;
After the execution of the statements above, the variable txt3
contains:
"What a very nice day"
52
JavaScript Operators
Comparison Operators
Comparison operators are used in logical statements to determine equality or
difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Comparison operators can be used in conditional statements to compare values
and take action depending on the result.
53
JavaScript Operators
Logical Operators
54
What you Learned
1 What is JavaScript
2 JavaScript Design
3 Using
JavaScript 4 Syntax
5 JavaScript
Objects
7 8