Lect 35 Javascript
Lect 35 Javascript
JavaScript programming language that can appear in html pages. It allow us to:
To dynamically create web pages To control a browser application Open and create new browser windows Download and display contents of any URL To interact with the user Ability to interact with HTML forms
You can process values provided by checkbox, text, textarea buttons
JavaScript
What is not possible with JavaScript
It is not possible to read and write files (security reasons) The only networking support it provides is:
It can send the contents of forms to a server and e-mail addresses It can cause the browser to load a web page
JavaScript
JavaScript Interpreter Process javaScript code To write JavaScript programs you need A web browser A text editor A JavaScript program can appear In a file by itself typically named with the extension .js In html files between a <script> and </script> tags Client-Side JavaScript the result of embedding a JavaScript interpreter in a web browser Example1 (See Example1.html) We use document.writeln to create the page contents Notice the html tags present in the writeln
JavaScript
When learning JavaScript we can identify two main areas Core JavaScript Set of rules specifying how to write JavaScript programs Client-Side JavaScript How JavaScript is used in web browsers A JavaScript program can appear In a file by itself in a file typically with the extension .js In html files between a <script> and </script> tags Example1 We use document.writeln to create the page contents Notice the html tags present in the writeln
As event handler
Example2 (See Example2.html)
JavaScript URLs
javascript:alert(Welcome)
<script> Embedding
You may place any number of JavaScript statements between <script> and </script> Statements are executed as the document is loaded <script> may appear in the head or body section of an html document Several pairs of nonoverlapping <script></script> blocks can appear in a document
All are consider part of the same program If you define a value in a block it can be referred from another block
<script> Embedding
language attribute (e.g., <script language=JavaScript></script>)
JavaScript is not the only language out there
Visual Basic Scripting Language (language = VBScript)
IE and Netscape will assume JavaScript if language is not specified. The language attribute allows you to specify a JavaScript version to use (e.g., <script language=JavaScript1.5> If a browser doesnt understand a language it will ignore the statements
10
JavaScript
Lets go over several basic constructs that allow us to define JavaScript programs. Some definitions string Any set of characters in double quotes ( ) function/method An entity that completes a particular task for us. It can takes values necessary to complete the particular task and it can return values. Generating Output with the document.writeln method Allow us to add text to the html file (see Example1) by providing the required text in You can specify html code and results of JavaScript constructs
11
JavaScript (Output)
Example 4 (See Example4.html) Illustrates how we can create a table using document.writeln Notice how we can use the Date() to specify a particular date format. Date() is part of JavaScript The + allow us to concatenate strings Notice how we have specified the border size. If you use then the table will not be generated. You need to use single quotes. Keep in mind that this example could have been written without using JavaScript. However you will see how by extending code similar to the one provided you can dynamically decide what your final html look like
12
JavaScript (Variables )
variable A memory location that can store a value. In JavaScript variables are declared using var var name; Variables names must start with a letter and can be followed by letters and digits A variable can hold different type of values Values we can assign to variables Integer 0, 10, 40, 6, -7 Floating-point 3.2, .67, 1.48E-20 String literals hello, goodbye Operators Typical arithmetic operators (+, -, *, /) Example 5 (See Example5.html)
13
14
JavaScript References
JavaScript The Definitive Guide by David Flanagan ISBN: 0-596-00048-0 JavaScript Pocket Reference by David Flanagan ISBN: 0-596-00411-7
15