JavaScript is a scripting language developed by Sun Microsystems and Netscape.. JavaScript effects and rules ► JavaScript can enhance the dynamics and interactivity of the site by usin
Trang 1The Foundation of JavaScript Syntax
Session 7
Trang 2 Describe JavaScript
Understand foundation of JavaScript syntax
Trang 3 JavaScript JavaScript makes makes it it easier easier to to create create
dynamic and interactive Web pages.
JavaScript is a scripting language developed by Sun Microsystems and Netscape.
JavaScript developed from Netscape's Livescript.
Where is JavaScript running ?
Trang 4JavaScript effects and rules
► JavaScript can enhance the dynamics and interactivity of the site by using its effects.
Provide User Interaction
Dynamically Change Content
Validate data
Provide Database Interaction
► Similar to any other language, JavaScript also follows some basic grammar rules like:
Using Caps JavaScript is case sensitive
Using Pairs Open and closed symbols need to work in pairs
Using Spaces JavaScript ignores extra white space
Using Comments make notes to yourself about what the script
is doing
Trang 5JavaScript Tools and Run- Time
Environment
►The JavaScript code-generating tools and IDE helps in creating useful JavaScript code A few of these include:
►Run Time Environment
Trang 6Client-Side JavaScript
►If a client-side script is embedded in the page, the server forwards the full content of the HTML document – The JavaScript statements and the HTML content
►When the browser receives the document, it executes the HTML and JavaScript statements without any interaction with the server
Trang 7Server-Side JavaScript
►We can embedded JavaScript statements in
an HTML document that are executed on the server
executable in bytecode format
executable, the run-time engine executes the server-side JavaScript statements and returns the HTML page to the browser
►Some of the uses of server-side scripts
Trang 8Embedding JavaScript in Web Page
document in the following ways :
Using SCRIPT tag:
<script language="JavaScript">
JavaScript statements;
// >
</script>
Using an external File
<script language="JavaScript" src="filename.js"> </script>
Using JavaScript Expressions within HTML Tag
Attribute Values
Using JavaScript in event handlers
Trang 9Program Using Msg box and write
method
Example:
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "Javascript">
confirm ("Are you Sure?");
Trang 10they are declared in the script
Global Variable
Local Variable
Trang 12Data Type - Example
Trang 13Literal - Types
hexadecimal and binary number system.
point or an “e” or “”E” followed by an integer.
enclosed in single or double quotation marks.
False.
Trang 14(operands) and return a new value
they perform like:
Trang 15Arithmetic Operator
(either literals or variables) as their operands and return a single numerical value
Trang 16Comparison Operator
and returns a logical value based on whether the comparison is true or not
Trang 17Logical Operators
multiple comparisons into a conditional
expression
Trang 18Logical Operators - Example
alert ("The value of x is "
+ x + "The value of y is " + y);
alert("x AND y = " + (x && y));
Trang 19String Operator
operands and creates a new string, which is same, as the two original strings run together
Example:
x = ‘yellow’;
y = ‘green’;
z = x + y + ‘white’;
which means z is “yellowgreenwhite”
w = y + 9, which means w is “green9”
Trang 20Evaluation Operator
Conditional operator
condition) ? trueVal : falseVal
Assigns a specified value to a variable if a condition is
true, otherwise assigns an alternate value if condition
is false
eg.
status = (age >= 18) ? "adult" : "minor“
Typeof operator
The typeof operator returns a string indicating the
type of the operand.
eg
var x = 5;
Trang 21Operator Precedence
► When there are several operators to be evaluated in
an expression, the precedence of the operator determines the sequence in which the operators are evaluated
► The following table lists the precedence of
operators, from lowest to highest:
Trang 22variables in different contexts
and operators which evaluates to a single value
Arithmetic: evaluates to a number
Logical: evaluates to a boolean value
String: evaluates to a string
together via operators
Trang 23Regular Expression
be used to match the character combinations of a string
character patters in a string input by the user
Simple patterns
Combination of simple and special characters
ways:
Using an object initializer
Trang 24Using Regular Expression
Expression includes:
Exec, Test, Match, Search, Replace, Split
of the object is:
Trang 25Regular Expression - Example
str = re.test ("Time and Tide
wait for none");
Trang 26the same name
value
arrayObjectName = new Array([element0, element1, ,
elementN])
we create it Eg emp[0] = "Ryan Dias"
Trang 27manipulate the array
Trang 28Conditional Statements
► A conditional statement is used to test a
condition The result determines the statement or block of statements to be executed.
► The various conditional statements include:
If… Else
Switch
Trang 30► JavaScript has several pre-defined functions that
we can use within the script.
► Some of pre-defined JavaScript functions