1.03k likes | 1.18k Views
JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak. Based on material from JavaScript Unleashed (2nd Ed). Introduction to JavaScript. Basics for Non Programmers. JavaScript. JavaScript is a Programming Language, although not a complete one.
E N D
JavaScript Programming an Introduction Prepared ByP .D. Krolak and M.S. Krolak Based on material from JavaScript Unleashed (2nd Ed)
Introduction to JavaScript Basics for Non Programmers
JavaScript • JavaScript is a Programming Language, although not a complete one. • JavaScript was developed by Netscape and is becoming an international standard, ECMA-262. • JavaScript is sent with the HTML document and is interpreted at the time it is loaded on the browser • JavaScript adds interaction
Embedding JS in HTML • The <script> </script> tag container • Accommodating the Non JS supporting browsers by using a comment to surround the content of the script within the container. • Viewing the JS code • Executing the scripts • loading a page • HTML enhancements
The <script> tag • <script language=“JavaScript1.x”> where x can be version of JavaScript used, i.e 0,1,2,3. If the browser does not support the version the tag and its content is ignored. • language can also be set to vb script, php, etc. • <Script > </Script> form the container • The script may be placed anywhere in the document but must exit before needed. • Most scripts are placed in the head and are thus loaded before they are needed for the display & interaction. • Scripts that contain document.write() must be in the body.
The <script> tag continued • JavaScript libraries may be stored external to the HTML document. • <script Language=javascript1.2 src=“Library’s_URL”> • The Library file extension must be “.js” • The External file does not have the <script></script> tags
Viewing the JS codeA Simple Hello World <Html> <Head> <Title>JS Sample</Title> </Head> <Body> <script Language=“javascript1.2”> document.write(“Hello World”); </Script> </Body> </Html> See the above web page
Viewing the JS codeA Confirm Hello World <Html> <Head> <Title>JS Sample</Title> </Head> <Body> <script Language=“javascript1.2”> confirm(“Hello World”); </Script> </Body> </Html> See the above web page
Viewing the JS codeAn Alert window -- Hello World <Html> <Head> <Title>JS Sample</Title> </Head> <Body> <script Language=“javascript1.2”> alert(“Hello World”); </Script> </Body> </Html> See the above web page
Accommodating the Non JS supporting browsers • The contents of the script container are commented so non JS browsers will ignore the contents. • <Script > <! -- // the comment is used by the non-JS Your JavaScript code here --> </Script>
Executing the scripts • The scripts are not executed in any necessary order. • They are executed as the flow of events dictates. Events are generally created by an action of the user interacting with the page elements. An example, a mouse over a link.
Loading a web page There is no difference in loading a page with a script or without one.
JavaScript Data Types • JavaScript does not have a rich set of data types like most widely used programming languages. • JavaScript is also not strongly typed.
JS Operators • Assignment Operators • Arithmetic Operators • Comparison Operators • String Operators • Conditional Operators • Boolean Operators • The type of operators
Assignment Assignment operator assigns a value to a variable.
Arithmetic Arithmetic operators take numeric variables and return a single numeric value
Comparison A comparison operator logically compares its operands and returns a logical True or False.
String Operators String operators are the same as comparison, using a sort sequence. This is called a lexicographic or dictionary sort String a> b, or a>=b etc. String has two forms of concatenation, i.e. (the string ‘b’ is added to the end of string ‘a’) • a+b • a +=b
Conditional Assign a value if the condition is true or the alternative value if not. (condition) ? True Value : False Value
Boolean A Boolean operator takes two operands and returns a truth value.
Typeof Operand Typeof operand returns the data type of the operand.
JS Operators (continued) • Function operators • Data structure operators • Bit wise operators • Operator precedence
Statements • Statements define the flow of the script. • JS statements require a “;” at the end if there are more than one on a line. • A block of statement that are executed in order is enclosed by curly brackets {}. • Flow is normally linear but may be altered by conditional, looping, or similar statements.
Comments • Comments are not really part of the program statement but are provided for the programmer and others as notes and reminders of what is happening in the JS. Statement; //single line comment /* this the way to create multi line comments */
JS Control Structures and Looping • Conditional statements • Looping statements • Labeled • with • switch