Compute Science Dept: Javascript
Compute Science Dept: Javascript
Compute Science Dept: Javascript
JavaScript
Lecture
Introduction
XHTML is limited in what it offers web suffers in terms of interactivity Web suffers can either click a hyperlink or fill in a form JavaScript makes Web page more dynamic by generating events that can results in many action
Introduction
The main two reasons that we use JavaScript in Web pages are
Dynamic Client-side execution
Introduction
JavaScript dynamic and visual effects including
Intercepting and processing mouse clicks Client-side execution including
Validating form input Processing client request that do not require server processing
Introduction
Client-side processing minimize client/server communication and traffic JavaScript is normally executed on client side, OR the server side.
Introduction
Like XHTML, JavaScript is an interpretive Its not a compiled language Browser has an interpreter that scan the JavaScript code and interpret it.
Introduction
JavaScript was developed by Netscape Microsoft has a clone of JavaScript called Jscript. Jscript is designed to run inside the IE Jscript is a carbon copy of Javascript.
Introduction
Two types of JavaScript exist
Client-side JavaScript- code that are executed on the client by the browser Server-side JavaScript stay on the server and can be executed only by the server.
WHERE TO
JavaScript can be embedded in head section or body section JavaScript in the body section is executed WHILE the page load JavaScript in the head section will be executed when CALLED
Generic template
<html> <head> <title>JavaScript Template </title> <script language=javascript> JavaScript code goes here </script> </head> <body> XHTML code goes here </body> </html>
JavaScript section
Embeding JavaScript
We prefer to embed JavaScript right before (or after ) the <head> tag closed This is a standard format Many web pages follow the same format JavaScript is included within the <body> section There is no limit on how many <script> tag can be embedded in the XHTML
Interpreter output
JavaScript Variables
JavaScript are use to hold values or expressions Rules for JavaScript variable names
Variable name are case sensitive Variable names must begin with a letter or underscore
JavaScript Variables
After declaration, variable is always empty It needs to be assign a values after decleration var x = 5; var carname=volvo; X=5; carname=volvo;
JavaScript Variables
Any computation can be done as normal if variable is already declared JavaScript operate in conditional statement like other programming language
} </script>
Prompt function
The prompt() function has the following function
Prompt(message, default) Message is a string that informs the user of what to input Default is a value that the user can accept instead of inputting a value.
Example codes
<html> <head> <title>Untitled Document</title> <script language="javascript"> input=prompt("Please enter a number", 7); </script> </head> <body> </body> </html>
Prompt function
Confirm function
The confirm() function is used to ask the user to confirm an input value It return a Boolean whose value depends on the user confirmation. If user confirm the input, it return true Otherwise it return false
Confirm function
General syntax is confirm(question) Where question is a string.
Confirm function
<html> <head> <title>Untitled Document</title> <script language="javascript">answer=confirm("You input:"+ 10+" is this correct?"); </script> </head> <body> </body> </html>
Confirm output
Output function
The output function in JavaScript are
document.write (string) Alert(string)
JavaScript Events
Events are actions that can be detected by JavaScript Examples of events
A mouse click A web page or an image load Mousing over a host on the web page Selecting an input field in an HTML form Submitting an HTML form
JavaScript Object
JavaScript id an Object Oriented Programming (OOP) An OOP language allows you to define your own object and make your own variable type.
JavaScript Properties
Properties are the values associated with an object <script type=text/javascript> var txt=Hellow World; document.write(txt.length); </script> Output 12 The values is Hellow Word
JavaScript Methods
Methods are actions that can be performed on objects <script type=text/javascript> var str=Hellow Word document.write(str.toUpperCase()) </sript> toUpperCase is a method used to converted small letter string to Upper case
JavaScript Validation
JavaScript can be used to validate data in HTML forms before sending off the content to the server Form data that can be checked by JavaScript could be
User left required field empty User entered invalid e-mail address User entered invalid date User entered numeric in text field