Java Script

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Java Script - Presentation Transcript

    1.  
    2. What is JavaScript
      • JavaScript was designed to add interactivity to HTML pages
      • JavaScript is a scripting language (a scripting language is a lightweight programming language)
      • A JavaScript consists of lines of executable computer code
      • A JavaScript is usually embedded directly into HTML pages
      • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
      • Everyone can use JavaScript without purchasing a license
    3. How javascript helps?
      • Javascript gives designer a programming tool
      • It can put dynamic text into HTML page
      • Can respond to events – mouse over, click, page load etc
      • Can read, write and manipulate HTML elements
      • Can help validate use input
      • Can detect version of browser
    4. First Javascript
      • <html>
      • <body>
        • <script type=&quot;text/javascript&quot;>
        • document.write(&quot;Hello World!&quot;)
        • </script>
        • </body>
      • </html>
      Script Tag Indicates browser that it’s script that needs to be executed Type attributes indicates what type of script is this? This is standard command to write stuff on webpage
    5. Place for Javascript
      • Java script can put either in ‘head’ section of html page or in ‘body’ section of html page.
      • If put in body section – the script will be executed automatically when the page is loaded
      • If it is put in head section – then script has to be called explicitly.
      • By putting script in head you ensure that script is loaded first even before any part page uses it
    6. Example
      • <html>
      • <head>
      • <script type=text/javascript>
      • </script>
      • </head>
      • <body>
      • </body>
      • </html>
      <html> <head> </head> <body> <script type=text/javascript> </script> </body> </html> Javascript in Head Javascript in body
    7. Javascript in separate file
      • Javascript can be present in separate file as well.
      • You only need to refer it in your html.
      • You would refer it in body if you want it to get executed on page load.
      • You would refer it in head if you want to execute it on an event.
      • Files containing javascript has .js extention
    8. Example
      • <html>
      • <head>
      • <script src= myjscript.js >
      • </script>
      • </head>
      • <body>
      • </body>
      • </html>
      <html> <head> </head> <body> <script src=myjscript.js > </script> </body> </html> Javascript in Head Javascript in body
    9. Javascript Quick Stuff
      • Variable
        • Need not put data type for variables.
        • Variables are interpreted based on the values assigned.
            • var x = 10;
            • var city = “Bangalore”
        • Variables names are case sensitive
      • if(condition)
      • {
      • }
      if(condition) { } else { }
    10. String Operations
      • Var x = &quot;It is lot of pressure&quot;
      • Var y = &quot;at Bredge&quot;
      • Var z = x+y => &quot;It is lot of pressure at Bredge&quot;
    11. Loops
      • For Loop:
      • for(x=0; x <= 10; x++)
      • {
      • }
      While Loop: while(condition { } Do-while Loop: do { } while(condition) For-In for(variable in object) { }
    12. For – In example
      • <html>
      • <body>
      • <script type=&quot;text/javascript&quot;>
      • var xvar mycars = new Array()
      • mycars[0] = &quot;Saab&quot;
      • mycars[1] = &quot;Volvo&quot;
      • mycars[2] = &quot;BMW &quot;
      • for (x in mycars)
      • {
      • document.write(mycars[x] + &quot;<br />&quot;)
      • }
      • </script>
      • </body>
      • </html>
    13. Javascript popup boxes
      • With javascript we can create popup boxes
      • Three types of popup boxes
        • Alert Box
        • Confirm Box
        • Prompt Box
    14. Example -AlertBox
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function disp_alert()
      • {
      • alert(&quot;I am an alert box!!&quot;)
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;disp_alert()&quot; value=&quot;Display alert box&quot; />
      • </body>
      • </html>
    15. Example Confirm Box
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function disp_confirm()
      • {
      • var r=confirm(&quot;Press a button&quot;)
      • if (r==true)
      • {
      • document.write(&quot;You pressed OK!&quot;)
      • }
      • else
      • {
      • document.write(&quot;You pressed Cancel!&quot;)
      • }
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;disp_confirm()&quot; value=&quot;Display a confirm box&quot; />
      • </body>
      • </html>
    16. Prompt Box
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function disp_prompt()
      • {
      • var name=prompt(&quot;Please enter your name&quot;,&quot;Harry Potter&quot;)
      • if (name!=null && name!=&quot;&quot;)
      • {
      • document.write(&quot;Hello &quot; + name + &quot;! How are you today?&quot;)
      • }
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;disp_prompt()&quot; value=&quot;Display a prompt box&quot; />
      • </body>
      • </html>
    17. Functions in JavaScript
      • Functions are written in head section
      • Functions do not have return type
      • However function can return values
      • Functions can take of arguments
          • function functionname ( var1,var2,...,varX )
          • {
          • some code
          • }
      • Functions are called upon document events such as onclick, mouseover and so on.
    18. Function Example
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function product(a,b)
      • {
      • return a*b
      • }
      • </script>
      • </head>
      • <body>
      • <script type=&quot;text/javascript&quot;>
      • document.write(product(4,3))
      • </script>
      • </body>
      • </html>
    19. Javascript events
      • Every element of web page such as <body>,<table>,<form>,<input> and so on have events associated with them.
      • When these events occur, a javascript piece of code or javascript function can be invoked
      • Some of the important events are
        • Mouse click
        • Webpage loading or image loading
        • Mouse over
        • Submitting a page
    20.  
    21. Example - Events
      • <html>
      • <head>
      • <script type=text/javascript>
      • function f1(msg)
      • {
      • alert(msg);
      • }
      • </script>
      • </head>
      • <body>
      • <b>This example also shows bit layouting. You lay out components using table with border=0</b>
      • <table>
      • <tr>
      • <td>Name</td> <td><input type='text' value=&quot;click me&quot; onclick=&quot;f1('you clicked on Name')&quot;></input></td>
      • </tr>
      • <tr>
      • <td>Age</td> <td><input type=text value=&quot;double click here&quot; ondblclick=&quot;f1('you double clicked on Age')&quot;> </input></td>
      • </tr>
      • <tr>
      • <td>Salary</td> <td><input type=text value=&quot;type somethig here&quot; onchange=&quot;f1('you typed some data in Salary')&quot;> </input></td>
      • </tr>
      • </table>
      • </body>
      • </html>
    22. onerror event
      • Onerror event is fired when there is a script error
      • Since script are not compiled, it will difficult catch typo and other mistakes.
      • Handling onerror will help identify the error. However this event is fired when the erroneous script is executed
      • To handle this error we need provide a function which handles it.
      • Onerror event provides three information – error message, url of the page where error is caused, line number where error is present.
      • The handling function should take three parameters
    23. Onerror Example
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • onerror=handleErr
      • var txt=&quot;&quot;
      • function handleErr(msg,url,l)
      • {
      • txt=&quot;There was an error on this page. &quot;
      • txt+=&quot;Error: &quot; + msg + &quot; &quot;
      • txt+=&quot;URL: &quot; + url + &quot; &quot;
      • txt+=&quot;Line: &quot; + l + &quot; &quot;
      • txt+=&quot;Click OK to continue. &quot;
      • alert(txt)
      • return true
      • }
      • function message()
      • {
      • alert(&quot;Welcome guest!&quot;)
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; />
      • </body>
      • </html>
    24. Accessing Elements of Page
      • Most often javascript need to access your elemets
        • to validate information
        • to Modify an attribute of an element
        • to Dynamically put the values
        • to add additional elements
      • Element could be any html element such as <body>, <table>,<input>, <form> etc.
    25. Accessing Elements of Page
      • All the elements in a html document are stores like tree with <html></html> being root node
      • Every element can be uniquely identified by giving unit id
          • <intpu type=text id= &quot; nameField &quot; >
      • All the attribute values can obtained using document object
      • var ele = document.getElementById( &quot; nameField &quot; )
      • Attribute values of element can be accessed using ‘element’ variable
          • ele.value()
    26. Accessing Elements - Example
      • <html>
      • <head>
      • <script type=text/javascript>
      • function infoRead()
      • {
      • ele1 = document.getElementById(&quot;name&quot;);
      • ele2 = document.getElementById(&quot;Age&quot;);
      • ele3 = document.getElementById(&quot;Salary&quot;);
      • alert(&quot;You have entered &quot;+
      • &quot;Name - &quot;+ele1.value +
      • &quot; Age - &quot; + ele2.value +
      • &quot; Salary - &quot;+ele3.value
      • )
      • }
      • </script>
      • </head>
      • <body>
      • <b>This example also shows bit layouting. You lay out components using table with border=0</b>
      • <table>
      • <tr>
      • <td>Name</td> <td><input type='text' id=&quot;name&quot;></input></td>
      • </tr>
      • <tr>
      • <td>Age</td> <td><input type=text id=&quot;Age&quot;> </input></td>
      • </tr>
      • <tr>
      • <td>Salary</td> <td><input type=text id=&quot;Salary&quot;> </input></td>
      • </tr>
      • </table>
      • <input type=submit onclick=&quot;infoRead()&quot; value=&quot;Sumit&quot;/>
      • </body>
      • </html>
    27. Javascript Objects
      • javascript is a object oriented language
      • You can create your own objects
      • Every object has property and methods
      • javascript also provide predefined objects
        • String object
        • Date Object
        • Array Object
        • Math Object
    28. Javascript DOM Objects
      • HTML DOM – HTML D ocument O bject M odel
      • It defines standard set of objects for HTML and standard way to access them
      • All HTML elements, their attributes and containing test can be manipulated using DOM
    29. DOM object list
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    SlideShare Zeitgeist 2009

    + siddaramsiddaram Nominate

    custom

    733 views, 2 favs, 2 embeds more stats

    Java Script presentation slide show

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 733
      • 724 on SlideShare
      • 9 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 0
    Most viewed embeds
    • 7 views on http://alex458-8.blogspot.com
    • 2 views on http://abiomedicalinformatics.blogspot.com

    more

    All embeds
    • 7 views on http://alex458-8.blogspot.com
    • 2 views on http://abiomedicalinformatics.blogspot.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories