Download presentation
Presentation is loading. Please wait.
Published byRoderick Ford Modified over 9 years ago
1
JavaScript
2
JavaScript is a programming language used to make web pages interactive. JavaScript is scripting language used for client side scripting. JavaScript developed by Netscape in 1995 Case sensitive
3
JavaScript Benefits of JavaScript JavaScript gives HTML designers a programming tool JavaScript can react to events Alert Messages JavaScript can be used to validate data JavaScript can be used to input Validation Disadvantages of JavaScript Depends on the browser
4
JavaScript Embedding JavaScript in HTML tag is used to insert a JavaScript into an HTML page document.write ("Hello");
5
JavaScript External file JavaScript can be put in a separate.js file An external.js file lets you use the same JavaScript on multiple HTML pages
6
JavaScript document.write (" Welcome to popo! Hello"); document.write()- method used to print text
7
JavaScript lastModified include the last update date on your page by using the following code: document.write("This page Last update:" + document.lastModified);
8
JavaScript bgColor and fgColor document.bgColor="red"; document.fgColor="blue";
9
JavaScript Data Types Basic types of data in JavaScript are strings, numbers, boolean and null. String- group of characters enclosed in double quote Number- integers and floating point values Boolean- true or false Null- represents nothing and has a special value, indicated by null
10
JavaScript Variables Variables are storage locations used to store date Variable can be declared as var variable name= value; Eg Var example=“this is a string”; var name="popo"; document.writeln ("My name " +name);
11
JavaScript Operators JavaScript uses “operators” allows to make mathematical calculations Assignment operators =, +=, -=, *=,/= Arithmetic operators +, -, *, /, % Logical operators &&, ||, != Comparison operators, >=, ==, != Conditional operator (exp1)?(exp2) : (exp3);
12
JavaScript var a=10; var b=20; var c=a+b; document.writeln ("sum of "+a+" + "+b+" = "+ c);
13
JavaScript var a=10; var b=20; var c=a+b; if(c%2==0){document.write("even");} else{document.write("odd");}
14
JavaScript loop statements: while ( condition ) statement ; do statement while ( condition ); for ( initialization ; condition ; increment ) statement ; var i; for(i=1;i<=40;i++) document.writeln(i);
15
JavaScript The switch statement: switch ( expression) { case label : statement ; break; case label : statement ; break;... default : statement ; }
16
JavaScript Alert() method Used to alert the user on some action, with some information Syn alert(“message”); alert("I am popo");
17
JavaScript Prompt() Method Prompt method displays a small dialog box with a provision for text entry along with 2 buttons Ok and Cancel The text entered in the box can be stored in a variable var age= prompt("enter age"); if(age>=20)alert("I am happy"); else alert("I am popo"); Can assign value var k=prompt("dfgsdf",“value");
18
JavaScript Confirm() method Enables the user to confirm var age=prompt("enter age"); if( confirm("is it greater than 20") )alert("I am happy"); else alert("I am popo");
19
JavaScript parseInt()- convert string to integer parseFloat()-convert string to float var a= parseInt(b)+parseInt( c); var a="10"; var b="20"; var c=a+b; alert(c); var d=parseInt(a)+parseInt(b); alert(d);
20
JavaScript Date function Date manipulations can be performed by the method of the Date object Create an instance of Date object Using different methods of Date object user can carry out date manipulations Some methods are
21
JavaScript Date methods getDateReturn day of the month as integer(1-31) getDayReturn day of the week as integer(0-6) getMonthReturn month as integer (0-11) getSecondsReturn seconds as integers (0-59) getYearReturn year as two digit integer setDateset the day of the month as integer(1-31)
22
JavaScript var d=new Date(); document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear()); Output -10/01/2013
23
JavaScript Functions Function can be defined function function name(arguments) { – Function body; } Eg function fact(num) { var fact=1; for(i=1;i<=num;i++) fact=fact*i; return fact; }
24
JavaScript function square(number) {alert( number * number);} document.write("The function returned ", square(5) );
25
JavaScript function square(number) {return number * number;} document.write("The function returned ", square(5)); The function returned 25.
26
JavaScript Event handler Event handlers can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc. Events are signals generated when specific action occur. Script can be written to react to these events.
27
JavaScript onClick onClick handlers execute only when users click on buttons, links, etc. function ss() { alert("Thank you popo!") } The function ss() is invoked when the user clicks the button. Note: Event handlers are not added inside the tags
28
JavaScript onDblclick Occurs when a mouse double-click function ss() { alert("Thank you popo!") }
29
JavaScript onMousedown Occurs when mouse button is pressed function ss() { alert("Thank you popo!") }
30
JavaScript onMousemove Occurs when mouse pointer moves function ss() { alert("Thank you popo!") }
31
JavaScript onMouseout Occurs when mouse pointer moves out of an element function ss() { alert("Thank you popo!") } popo
32
JavaScript onkeypress Occurs key is pressed and released function ss() { alert("Thank you popo!") }
33
JavaScript onsubmit Occurs when a form is submitted function ss() { alert("Thank you popo!") }
34
JavaScript onload Occurs when a page is loaded function ss() { alert("popo"); }
35
JavaScript onmouseover Occurs when mouse pointer moves over an element function ss() { document.write("popo"); } Over Here!
36
JavaScript unload Occurs when user leaves a page
37
JavaScript Addition of 2 nos (each Nos in 2 text, result in result text, with a button) function calcu(f) { f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value); } Enter first no : Enter sec no:
38
JavaScript All arithmetic operation with 4 buttons & 3 text function add(f){ f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va lue); } function sub(f){ f.ans.value=parseInt(f.first.value)- parseInt(f.sec.value); } function mult(f){ f.ans.value=parseInt(f.first.value)*parseInt( f.sec.value); } function div(f){ f.ans.value=parseInt(f.first.value)/parseInt( f.sec.value); } Enter first no : Enter sec no: Answer :
39
JavaScript Print First n nos function pr(f) { var n=parseInt(f.no.value); for(i=1;i<=n;i++) document.write(i+" "); } Enter limt no :
40
JavaScript
41
Check validation function validate() { if(document.login.userName.value=="") { alert ("Please enter User Name") } if(document.login.password.value=="") {alert ("Please enter Password") } }
42
JavaScript
43
JavaScript II Array Different ways to declare Array 1) var colors = ["red", "green", "blue"]; colors[0] is "red" 2) new Array() - to create an empty array: – var colors = new Array(); – Add elements to the array later: colors[0] = "red"; – colors[1]="green"; colors[2] = "blue"; 3) new Array( n ) to create an array of that size – var colors = new Array(3);
44
JavaScript II Array example var colors = ["red", "green", "blue"]; for(i=0;i<3;i++) document.write(colors[i]+" ");
45
JavaScript II Array Example var colors = new Array(); for(i=0;i<10;i++) { colors[i]="color"+i; document.write(colors[i]+" "); }
46
JavaScript
47
File popo.js Contents: function popup() { alert("Hello popo") } -------------------------------------------------------------------------------------- HTML & JavaScript Code:
48
JavaScript
49
javaScript Print Script - window.print() The JavaScript print function window.print() will print the current webpage when executed.
50
JavaScript
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.