Embed presentation
Downloaded 25 times










![Arrays • Declaration: are objects in JavaScript a= new Array(3) a[0]=1 a[1]=2 a[2]=3 • A single array can hold any kind of data junk= new Array(3) junk[0]=“Sunday” junk[1]=0 junk[2]=true • Array length a.length • Array size is incremented dynamically a= new Array() a[4]=4 a.length is 5 • Initialized array week=new Array(“sun”,”mon”,”tue”,”wed”,”thu”,”fri”,”sat”) document.write(week[0]) sun document.write(week) sun, mon, tue, wed … • Array of array: matrix= new Array(new Array(1,2,3), new Array(4,5,6) a[0][1] 2](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-11-2048.jpg&f=jpg&w=240)

![•Calling a function function display(x){ if(x==null) x=“Greetings” document.write(x) } Can be called as : display() or display(“hello”) No overloading possible. If overloaded functions are provided, only the last function is considered •Function arguments: arguments array can be used to collect the arguments of a function. <script language=“JavaScript”> <!-- function sum(){ total=0 for(j=0;j<sum.arguments.length;j++){ total+=sum.arguments[j]; document.write(sum)} --></script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-13-2048.jpg&f=jpg&w=240)
![•Local and Global variables Local variables are created using var <!-- function sum(){ total=0 var total=0 then it is available only in the function for(j=0;j<sum.arguments.length;j++){ total+=sum.arguments[j]; } --></script> …< script> <!— document.write(total) // ok doesn't display if local variable --> </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-14-2048.jpg&f=jpg&w=240)


![window object •window is the default object that is available to the JavaScript. •document.write document is an object inside window and instead of document.write we can also write window.document.write implying write on current window’s document. •Properties: name, self, top, parent, opener, defaultStatus, status, closed, length Methods: alert(displayString), String prompt(question,defaultanswer), boolean confirm(question), Timer setTimeOut(expression, millsecs), clearTimeOut(timerobj), blur(), focus(), open(url,name,[options]), close() Events: onLoad, onUnload, onFocus, onBlur, OnError [options]: menubar=,toolbar,status, width=, height=](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-17-2048.jpg&f=jpg&w=240)
![Communicating with user <html><head> <script> function communicate(){ alert(“Hello”) s=prompt(“What is your name”, “xyz”) b=confirm(“Do you want to see your name displayed in colors”) a=new Array(“RED”,”GREEN”,”BLUE”,”YELLOW”, “BLACK”,”PURPLE) while(b){ document.write(“<font color=“+a[i]+”>”+s+”</font>”) if(i++>a.length)i=0 b=confirm(“Do you want to continue”)}} </script> </head><body onUnload=“alert(‘Bye!’)”> <script> communicate()</script> </body></html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-18-2048.jpg&f=jpg&w=240)

![Timer <html><head> <script> a=new Array(“Welcome”,”to”,”jis”) i=0 function setTimer(){ setTimeout(“display()”,1000); } function display(){ status=a[i] i=i++%3 setTimer() }](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-20-2048.jpg&f=jpg&w=240)



![<html><head> <title>Shop with us</title> <script> function display(x){ switch(x){ case 'left': parent.b.l.location.href="img.jpg" break case 'right': parent.b.frames[1].location.href="img.jpg" break case 'top': top.location.href="img.jpg" break case 'bottom' : parent.b.location.href="img.jpg" break case 'self' : location.href="img.jpg" break default: location.href=history.back() }} </script></head>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-24-2048.jpg&f=jpg&w=240)

![document Properties: images[], forms[], links[], anchors[],bgColor, fgColor, title, linkColor, alinkColor, vlinkColor Methods: open([mimetype]), write(expr1), close() Example 1:bgColor and fgColor <body onFocus=“bgColor=‘white’;fgColor=‘black’” onBlur=“bgColor=‘black’;fgColor=‘black’”> Example 2:Generating a document <html><head><script> function generate(){ win=open("","gen") win.document.open("texthtml") win.document.write("<html><body onLoad=alert('ok')><U>Welcome</U>") win.document.write("</body></html>") win.document.close() } </script> <body><form><input type=‘button’ onClick=‘generate()’></form></body> </html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-26-2048.jpg&f=jpg&w=240)
![images Properties:border, height, width, src, name, complete Creating new Image object: im=new Image() im=new Image(40,50) Events: onLoad, onError, onAbort, onMouseOver, onMouseOut, onDblClick Example 1: <html><head> <script> i=0 imgs=new Array("image1.jpg","image2.jpg","image3.jpg","image4.jpg","image5.jpg") function change(){ document.images[0].src=imgs[i++ % imgs.length]} </script></head> <body><img src="image5.jpg" onMouseOver="change()"> </body> </html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-27-2048.jpg&f=jpg&w=240)
![On the internet it takes time to download the image. So to check if the image has been downloaded and do the required we need image as object. <script> i=0 imgs=new Array(5) for(i=0;i<5;i++){ imgs[i]=new Image() imgs[i].src="image"+ (i+1)+".jpg“ } i=0 function set(){ setTimeout('change()',1000) } function change(){ if(imgs[i].complete){ document.images[0].src=imgs[i++ % imgs.length].src set()}} </script> </head> <body><img src="image5.jpg"> <script>set()</script></body></html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-28-2048.jpg&f=jpg&w=240)
![form Properties: action, method, name, elements[], target Events: onSubmit, onReset Form elements: Events:All form elements: onBlur, onFocus Select,text, textarea: onChange Text, texrarea: onSelect Button,submit,reset,radio,checkbox: onClick Button: onMouseDown, onMouseUp, TextArea: onKeyDown, onKeyUp, onKeyPress Properties: All : name, type, value (except select) radio, checkbox: checked, defaultChecked select: length, options[], selectedIndex text, password, textarea:defaultValue Methods: All form elements: focus(), blur() button,submit, reset: click()](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-29-2048.jpg&f=jpg&w=240)
![Example 1: Working with text, radio and checkbox <html><head><title>Validate</title><script> <!-- function check(){ with(document.forms[0]){ if ((name.value=="") ){ alert("Please ensure that all fields are filled up") return false } if(like[0].checked) s= "Thankyou, "+name.value +"." else s="Sorry !" s=s+" As per your suggestion we shall look into areas:("; for(i=0;i<better.length;i++) if (better[i].checked) s=s+ better[i].value+"," s=s+" and more ) for further improvements " } alert(s) return true}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-30-2048.jpg&f=jpg&w=240)

![Example 2: Working with select <html><head><script> <!-- function check(){ i=document.f1.choose.options.selectedIndex; if(i!=0){ if(i==1) alert("Correct"); else alert("Your choice, "+ document.f1.choose.options[i].text +"- is incorrect"); }} //--> </script></head> <body> <form name=f1> Which of the following is not true about JavaScript? <select name="choose" onChange="check()">](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-32-2048.jpg&f=jpg&w=240)

![link, anchor and history links, anchors: array properties: href, hostname, pathname, port, target, protocol Events: onClick, onDblClick, onKeyDown, onKeyUp, onKeyPress, onMouseDown, onMouseUp, onMouseOver, onMouseOut Example: Game <html><head> <script> arr=new Array(‘one.hif’,’two.gif’) i=0 im=“” function f(x){ r=Math.floor(Math.random()*2) document.images[x].src=arr[r] if(i==0) im=arr[r] if(i++>0 && im==arr[r]) alert(“You have won in “+ i + “ attempts”) } </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-34-2048.jpg&f=jpg&w=240)


![Date object var dt= new Date(); Creates a new date object which contains system’s current date and time. Methods: getDate() getMonth() getYear() getHours() getMinutes() getSeconds() setDate(value) setMonth(value) setYear(value) setHours(value) setMinutes(value) setSeconds(value) toGMTString() toLocalString() Example 1: <html><head><title>time</title><script> <!-- function setTime() { dt= new Date(); str=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds(); document.forms[0].time.value=str; timer=setTimeout("setTime()",1000); } </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-37-2048.jpg&f=jpg&w=240)




This document provides an overview of JavaScript fundamentals including a brief history of JavaScript, its uses, language features, inclusion of scripts in HTML documents, and the JavaScript object model. It discusses the window object and properties like location, frames, history. It also covers variables and data types, operators, control structures, arrays, functions, and communicating with the user through prompts, alerts and changing the status bar.










![Arrays • Declaration: are objects in JavaScript a= new Array(3) a[0]=1 a[1]=2 a[2]=3 • A single array can hold any kind of data junk= new Array(3) junk[0]=“Sunday” junk[1]=0 junk[2]=true • Array length a.length • Array size is incremented dynamically a= new Array() a[4]=4 a.length is 5 • Initialized array week=new Array(“sun”,”mon”,”tue”,”wed”,”thu”,”fri”,”sat”) document.write(week[0]) sun document.write(week) sun, mon, tue, wed … • Array of array: matrix= new Array(new Array(1,2,3), new Array(4,5,6) a[0][1] 2](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-11-2048.jpg&f=jpg&w=240)

![•Calling a function function display(x){ if(x==null) x=“Greetings” document.write(x) } Can be called as : display() or display(“hello”) No overloading possible. If overloaded functions are provided, only the last function is considered •Function arguments: arguments array can be used to collect the arguments of a function. <script language=“JavaScript”> <!-- function sum(){ total=0 for(j=0;j<sum.arguments.length;j++){ total+=sum.arguments[j]; document.write(sum)} --></script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-13-2048.jpg&f=jpg&w=240)
![•Local and Global variables Local variables are created using var <!-- function sum(){ total=0 var total=0 then it is available only in the function for(j=0;j<sum.arguments.length;j++){ total+=sum.arguments[j]; } --></script> …< script> <!— document.write(total) // ok doesn't display if local variable --> </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-14-2048.jpg&f=jpg&w=240)


![window object •window is the default object that is available to the JavaScript. •document.write document is an object inside window and instead of document.write we can also write window.document.write implying write on current window’s document. •Properties: name, self, top, parent, opener, defaultStatus, status, closed, length Methods: alert(displayString), String prompt(question,defaultanswer), boolean confirm(question), Timer setTimeOut(expression, millsecs), clearTimeOut(timerobj), blur(), focus(), open(url,name,[options]), close() Events: onLoad, onUnload, onFocus, onBlur, OnError [options]: menubar=,toolbar,status, width=, height=](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-17-2048.jpg&f=jpg&w=240)
![Communicating with user <html><head> <script> function communicate(){ alert(“Hello”) s=prompt(“What is your name”, “xyz”) b=confirm(“Do you want to see your name displayed in colors”) a=new Array(“RED”,”GREEN”,”BLUE”,”YELLOW”, “BLACK”,”PURPLE) while(b){ document.write(“<font color=“+a[i]+”>”+s+”</font>”) if(i++>a.length)i=0 b=confirm(“Do you want to continue”)}} </script> </head><body onUnload=“alert(‘Bye!’)”> <script> communicate()</script> </body></html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-18-2048.jpg&f=jpg&w=240)

![Timer <html><head> <script> a=new Array(“Welcome”,”to”,”jis”) i=0 function setTimer(){ setTimeout(“display()”,1000); } function display(){ status=a[i] i=i++%3 setTimer() }](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-20-2048.jpg&f=jpg&w=240)



![<html><head> <title>Shop with us</title> <script> function display(x){ switch(x){ case 'left': parent.b.l.location.href="img.jpg" break case 'right': parent.b.frames[1].location.href="img.jpg" break case 'top': top.location.href="img.jpg" break case 'bottom' : parent.b.location.href="img.jpg" break case 'self' : location.href="img.jpg" break default: location.href=history.back() }} </script></head>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-24-2048.jpg&f=jpg&w=240)

![document Properties: images[], forms[], links[], anchors[],bgColor, fgColor, title, linkColor, alinkColor, vlinkColor Methods: open([mimetype]), write(expr1), close() Example 1:bgColor and fgColor <body onFocus=“bgColor=‘white’;fgColor=‘black’” onBlur=“bgColor=‘black’;fgColor=‘black’”> Example 2:Generating a document <html><head><script> function generate(){ win=open("","gen") win.document.open("texthtml") win.document.write("<html><body onLoad=alert('ok')><U>Welcome</U>") win.document.write("</body></html>") win.document.close() } </script> <body><form><input type=‘button’ onClick=‘generate()’></form></body> </html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-26-2048.jpg&f=jpg&w=240)
![images Properties:border, height, width, src, name, complete Creating new Image object: im=new Image() im=new Image(40,50) Events: onLoad, onError, onAbort, onMouseOver, onMouseOut, onDblClick Example 1: <html><head> <script> i=0 imgs=new Array("image1.jpg","image2.jpg","image3.jpg","image4.jpg","image5.jpg") function change(){ document.images[0].src=imgs[i++ % imgs.length]} </script></head> <body><img src="image5.jpg" onMouseOver="change()"> </body> </html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-27-2048.jpg&f=jpg&w=240)
![On the internet it takes time to download the image. So to check if the image has been downloaded and do the required we need image as object. <script> i=0 imgs=new Array(5) for(i=0;i<5;i++){ imgs[i]=new Image() imgs[i].src="image"+ (i+1)+".jpg“ } i=0 function set(){ setTimeout('change()',1000) } function change(){ if(imgs[i].complete){ document.images[0].src=imgs[i++ % imgs.length].src set()}} </script> </head> <body><img src="image5.jpg"> <script>set()</script></body></html>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-28-2048.jpg&f=jpg&w=240)
![form Properties: action, method, name, elements[], target Events: onSubmit, onReset Form elements: Events:All form elements: onBlur, onFocus Select,text, textarea: onChange Text, texrarea: onSelect Button,submit,reset,radio,checkbox: onClick Button: onMouseDown, onMouseUp, TextArea: onKeyDown, onKeyUp, onKeyPress Properties: All : name, type, value (except select) radio, checkbox: checked, defaultChecked select: length, options[], selectedIndex text, password, textarea:defaultValue Methods: All form elements: focus(), blur() button,submit, reset: click()](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-29-2048.jpg&f=jpg&w=240)
![Example 1: Working with text, radio and checkbox <html><head><title>Validate</title><script> <!-- function check(){ with(document.forms[0]){ if ((name.value=="") ){ alert("Please ensure that all fields are filled up") return false } if(like[0].checked) s= "Thankyou, "+name.value +"." else s="Sorry !" s=s+" As per your suggestion we shall look into areas:("; for(i=0;i<better.length;i++) if (better[i].checked) s=s+ better[i].value+"," s=s+" and more ) for further improvements " } alert(s) return true}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-30-2048.jpg&f=jpg&w=240)

![Example 2: Working with select <html><head><script> <!-- function check(){ i=document.f1.choose.options.selectedIndex; if(i!=0){ if(i==1) alert("Correct"); else alert("Your choice, "+ document.f1.choose.options[i].text +"- is incorrect"); }} //--> </script></head> <body> <form name=f1> Which of the following is not true about JavaScript? <select name="choose" onChange="check()">](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-32-2048.jpg&f=jpg&w=240)

![link, anchor and history links, anchors: array properties: href, hostname, pathname, port, target, protocol Events: onClick, onDblClick, onKeyDown, onKeyUp, onKeyPress, onMouseDown, onMouseUp, onMouseOver, onMouseOut Example: Game <html><head> <script> arr=new Array(‘one.hif’,’two.gif’) i=0 im=“” function f(x){ r=Math.floor(Math.random()*2) document.images[x].src=arr[r] if(i==0) im=arr[r] if(i++>0 && im==arr[r]) alert(“You have won in “+ i + “ attempts”) } </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-34-2048.jpg&f=jpg&w=240)


![Date object var dt= new Date(); Creates a new date object which contains system’s current date and time. Methods: getDate() getMonth() getYear() getHours() getMinutes() getSeconds() setDate(value) setMonth(value) setYear(value) setHours(value) setMinutes(value) setSeconds(value) toGMTString() toLocalString() Example 1: <html><head><title>time</title><script> <!-- function setTime() { dt= new Date(); str=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds(); document.forms[0].time.value=str; timer=setTimeout("setTime()",1000); } </script>](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fjavascript-141013052105-conversion-gate01%2f75%2fJava-script-37-2048.jpg&f=jpg&w=240)


