Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Script Introduction. Java Script Introduction.

Similar presentations


Presentation on theme: "Java Script Introduction. Java Script Introduction."— Presentation transcript:

1

2 Java Script Introduction

3 Java Script lightweight, interpreted programming language
designed for creating network-centric applications integrated with HTML implementations allow client-side script to interact with the user and make dynamic pages. first known as LiveScript, but Netscape changed its name to JavaScript Client-side

4 JavaScript can be implemented using JavaScript statements that are placed within the <script>... </script> Where to place the script tag Within body tag Within head tag External javascript file normally recommended that you should keep it within the <head> tags. <script ...> JavaScript code </script> script tag takes two important attributes Language Type <script language="javascript" type="text/javascript"> JavaScript code </script>

5

6 JavaScript is a case-sensitive language
Comment <! //--> document.write writes a string into our HTML document <html> <body> <script language="javascript" type="text/javascript"> document.write("Hello World!") </script> </body> </html> JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs JavaScript is a case-sensitive language

7 Semicolons are Optional
<script language="javascript" type="text/javascript"> var1 = 10 var2 = 20 </script> Semicolons are Optional Simple statements in JavaScript are generally followed by a semicolon character,. however, allows you to omit this semicolon if each of your statements are placed on a separate line <script language="javascript" type="text/javascript"> var1 = 10; var2 = 20; </script>

8

9 JavaScript in <head>...</head> section
<html> <head> <script type="text/javascript"> function sayHello() { alert("Hello World") } </script> </head> <body> <input type="button" onclick="sayHello()" value="Say Hello" /> </body> </html>

10 JavaScript in <body>...</body> section
<html> <head> </head> <body> <script type="text/javascript"> document.write("Hello World") </script> <p>This is web page body </p> </body> </html>

11 JavaScript in External File
1.Html <html> <head> <script type="text/javascript" src=“a.js" ></script> </head> <body> <input type="button" onclick="sayHello()" value="Say Hello" /> </body> </html> a.js function sayHello() { alert("Hello World") }

12

13

14 JavaScript Datatypes three primitive data types
Numbers, eg. 123, etc. Strings of text e.g. "This text string" etc. Boolean e.g. true or false. Non primitive data types Object Array Regular Expression Keyword is var Var can hold any type of values E.g. var a=40; var b=“hi”;

15 Java Script Variables Must start with letter, digits can be followed
Can Start with underscore Case sensitive variable names should not start with a numeral JavaScript variables have only two scopes. Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function. Local E.g. <script> function a() { var a=40; } Global E.g. <script> var d=10; function a() { var a=40; }

16 Example <html> <body onload = checkscope()> <script type = "text/javascript"> var myVar = "global"; function checkscope( ) { var myVar = "local"; document.write(myVar); } </script> </body> </html>

17 JavaScript - Operators
Arithmetic Operators Comparison Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators

18 Arithmetic Operators <html> <body> <script type="text/javascript"> var a = 33; var b = 10; var c = "Test"; var linebreak = "<br />"; document.write("a + b = "); result = a + b; document.write(result); document.write(linebreak); </script> Set the variables to different values and then try... </body> </html>

19 typeof Operator <html> <body> <script type="text/javascript"> var a = 10; var b = "String"; var linebreak = "<br />"; result = (typeof b == "string" ? "B is String" : "B is Numeric"); document.write("Result => "); document.write(result); document.write(linebreak); result = (typeof a == "string" ? "A is String" : "A is Numeric"); document.write(result); document.write(linebreak); </script> </body> </html>

20

21 JavaScript - Statement
If else <script type="text/javascript"> var age = 20; if( age > 18 ) { document.write("<b>Qualifies for driving</b>"); } </script> For <script type="text/javascript"> var count; document.write("Starting Loop" + "<br />") for(count = 0; count < 10; count++) { document.write("Current Count : " + count ); document.write("<br />"); } document.write("Loop stopped!"); </script>

22 Switch <script type="text/javascript"> var grade='A'; document.write("Entering switch block<br />"); switch (grade) { case 'A': document.write("Good job<br />"); break; case 'B': document.write("Pretty good<br />"); case 'C': document.write("Passed<br />"); case 'D': document.write("Not so good<br />"); case 'F': document.write("Failed<br />"); default: document.write("Unknown grade<br />") } document.write("Exiting switch block"); </script>

23 JavaScript - Functions
Function Definition function keyword followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces. Syntax : function functionname(parameter-list) { statements } function sayHello() { alert("Hello there"); }

24 JavaScript - Functions
Calling a Function <html> <head> <script type="text/javascript"> function sayHello() { document.write ("Hello there!"); } </script> /head> <body> <p>Click the following button to call the function</p> <form> <input type="button" onclick="sayHello()" value="Say Hello"> </form> <p>Use different text in write method and then try...</p> </body> </html>

25 JavaScript - Functions
<html> <head> <script type="text/javascript"> function sayHello(name, age) { document.write (name + " is " + age + " years old."); } </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type="button" onclick="sayHello('Zara', 7)" value="Say Hello"> </form> <p>Use different parameters inside the function and then try...</p> </body> </html>

26 return Statement <head> <body>
<html> <head> <script type="text/javascript"> function concatenate(first, last) { var full; full = first + last; return full; } function secondFunction() var result; result = concatenate('Zara', 'Ali'); document.write (result ); </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type="button" onclick="secondFunction()" value="Call Function"> </form> <p>Use different parameters inside the function and then try...</p> </body> </html>

27

28 JavaScript - Events JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable. Events are a part of the Document Object Model (DOM) onclick Event Type onsubmit Event type onmouseover and onmouseout

29 which occurs when a user clicks the left button of his mouse
onclick Event Type which occurs when a user clicks the left button of his mouse <html> <head> <script type="text/javascript"> <!-- function sayHello() { alert("Hello World") } </script> </head> <body> <p>Click the following button and see result</p> <form> <input type="button" onclick="sayHello()" value="Say Hello" /> </form> </body> </html>

30 onmouseover and onmouseout
<html> <head> <script type="text/javascript"> function over() { document.write ("Mouse Over"); } function out() { document.write ("Mouse Out"); </script> </head> <body> <p>Bring your mouse inside the division to see the result:</p> <div onmouseover="over()" onmouseout="out()"> <h2> This is inside the division </h2> </div> </body> </html>

31

32 6 + 4 = 210 9 + 2 = 711 8 + 5 = 313 5 + 2 = 37 7 + 6 = 113 9 + 8 = 117 = 416 = 1218 ?? + ?? = 123


Download ppt "Java Script Introduction. Java Script Introduction."

Similar presentations


Ads by Google