Intro to JavaScript

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

    Favorites, Groups & Events

    Intro to JavaScript - Presentation Transcript

    1. Introduction to JavaScript Jussi Pohjolainen Tampere University of Applied Sciences
    2. JavaScript
      • Object-orientated scripting language
      • Dialect of EcmaScript-standard
      • History
        • Netscape: LiveScript to JavaScript
        • Microsoft: JScript
        • Standard: EcmaScript
      • Latest version: JavaScript 1.8.1, a superset of EcmaScript
    3. Possibilities?
      • JS was designed to add interactivity to HTML pages
      • Dynamic HTML
      • Can react to events: page has finished loading, user clicks..
      • Data validation
      • Browser detection
      • Cookies
    4. Compatibility
      • Old or rare browsers
      • PDA or Mobile phones
      • JavaScript execution disabled
      • The use of speech browser
      • Browser incompatibilites
    5. JavaScript Today: AJAX
      • JavaScript is heavily used in AJAX-based sites
      • AJAX: asynchronous JavaScript and XML
        • group of interrelated techniques used on the client-side to create rich web apps where data is retrieved from the server in the background.
      • Example usage: Gmail, Google Maps
    6. Google Web Toolkit
      • Great tool for creating AJAX/JS-based sites
      • Coding is done with Java which is compiled to JavaScript
      • Resolves browser incompatibilies
      • See Example:
        • http://gwt.google.com/samples/Showcase/Showcase.html
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title >Embed Example </title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • </head>
      • <body>
      • <p>
      • <!-- See: http://covertprestige.info/html/script-syntax/ -->
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • document.write(&quot;Hello from JS!&quot;);
      • //]]>
      • </script>
      • </p>
      • </body>
      • </html>
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title> External JS Example </title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • <script type=&quot;text/javascript&quot; src=&quot;event.js&quot;></script>
      • </head>
      • <body onload=&quot;message()&quot; >
      • </body>
      • </html>
      • // event.js
      • function message()
      • {
      • alert(&quot;This alert box was called with the onload event&quot;);
      • }
    7. Result
    8. QUICK INTRO TO PROGRAMMING WITH JS
    9. Variables
      • Values are stored in variables
      • Variables are declared:
        • var carname;
      • Assigning value
        • carname = &quot;volvo&quot;;
      • Together
        • var carname = &quot;volvo&quot;;
      • ...
      • <body>
      • <p>
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • // Declaration
      • var car;
      • // Assigning
      • car = &quot;Volvo&quot;;
      • document.write(car);
      • //]]>
      • </script>
      • </p>
      • </body>
      • </html>
    10. Comparison (w3schools)
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • var d = new Date();
      • var time = d.getHours();
      • if ( time < 10 )
      • {
      •   document.write(&quot;<b>Good morning</b>&quot;);
      • }
      • //]]>
      • </script>
    11. Comparison (w3schools)
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • var d = new Date();
      • var time = d.getHours();
      • if ( time < 10 )
      • {
      •   document.write(&quot;<b>Good morning</b>&quot;);
      • }
      • else
      • {
      •   document.write(&quot;<b>Good Day</b>&quot;);
      • }
      • //]]>
      • </script>
    12. Repeat (w3schools)
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • var i=0;
      • while (i<=5)
      • {
      •   document.write(&quot;The number is &quot; + i);
      •   document.write(&quot;<br />&quot;);
      •   i = i + 1;
      • }
      • //]]>
      • </script>
    13. Popup Boxes
      • Alert Box
        • alert(&quot;some text&quot;);
      • Confirm Box
        • confirm(&quot;some text&quot;);
      • Prompt Box
        • prompt(&quot;sometext&quot;, &quot;default value&quot;)
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title>Embed Example</title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;alert('moi');&quot; value=&quot;Show alert box&quot; />
      • </body>
      • </html>
    14. Result
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title>Embed Example</title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • function showAlert()
      • {
      • alert(&quot;Hello World!&quot;);
      • }
      • //]]>
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot; showAlert(); &quot; value=&quot;Show alert box&quot; />
      • </body>
      • </html>
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title>Embed Example</title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • function askQuestion()
      • {
      • var name = prompt(&quot;Please enter your name&quot;,&quot;Harry Potter&quot;);
      • if ( name!=null && name!=&quot;&quot; )
      • {
      • alert(&quot;Hello &quot; + name + &quot;! How are you today?&quot;);
      • }
      • }
      • //]]>
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;askQuestion();&quot; value=&quot;Question for me&quot; />
      • </body>
      • </html>
    15. JS EVENTS AND DOM
    16. JS Events
      • Mouse click (onclick)
      • Web page loading (onload)
      • Mousing over and out (onmouseover onmouseout)
      • Submitting HTML form (onsubmit)
    17. About Events
      • You may cancel some events:
        • <a href= http://www.tamk.fi/ onclick=&quot;alert('message'); return false;&quot;>
      • Example
        • <form name=&quot;myform&quot; action=&quot;&quot; onsubmit=&quot;return validate();&quot;>
    18. Example
      • <form name=&quot;myform&quot; method=&quot;post&quot; onsubmit=&quot;return count()&quot;>
      • Height (m):<br/>
      • <input type=&quot;text&quot; name=&quot;height&quot;/><br/>
      • Weight (kg):<br/>
      • <input type=&quot;text&quot; name=&quot;weight&quot;/><br/>
      • <input type=&quot;submit&quot; value=&quot;BMI&quot;/><br/>
      • BMI<br/>
      • <input type=&quot;text&quot; name=&quot;result&quot;/>
      • </form>
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • function count()
      • {
      • var height = document.myform.height.value;
      • var weight = document.myform.weight.value;
      • document.myform.result.value = (weight / (height*height));
      • return false;
      • }
      • //]]>
      • </script>
    19. Result
    20. DOM
    21. DOM?
      • Specification how to access (X)Html – elements
      • Different levels of DOM: 0, 1, and 2
    22. window - object
      • Every reference to other objects is done via the window – object
      • You don't have to use the reference in your code:
        • window.document.form.height.value =
        • document.form.height.value
      • Window methods
        • alert, close, confirm, open, prompt, setTimeOut
    23. Opening new Browser Window
        • // See: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
        • window.open(&quot;http://www.tamk.fi&quot;,
        • &quot;title&quot;,
        • &quot;width=600, height=100&quot;);
    24. navigator - object
      • navigator tells information about your browser
      • Client-sniffing
        • var browser = navigator.appName;
        • var b_version = navigator.appVersion;
        • var version = parseFloat(b_version);
        • document.write(&quot;Browser name: &quot;+ browser);
        • document.write(&quot;<br />&quot;);
        • document.write(&quot;Browser version: &quot;+ version);
    25. document - object
      • Collection of elements in the html-page
      • Creating Nodes
        • createElement(&quot;element name&quot;)
        • createTextNode(&quot;text&quot;)
      • Walk the Tree
        • getElementsByTagName
        • getElementById
      • See: http://www.howtocreate.co.uk/tutorials/javascript/domstructure
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title></title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • function change()
      • {
      • // Get list of ALL <h1> - elements
      • var listOfHeading1 = window.document.getElementsByTagName(&quot;h1&quot;);
      • // Find the first <h1> - element in the list
      • var heading1 = listOfHeading1[0];
      • // Get the child - element of the first <h1> - element (Text)
      • var text = heading1.firstChild;
      • // Replace the text
      • text.data = &quot;Hello from JS!&quot;;
      • }
      • //]]>
      • </script>
      • </head>
      • <body>
      • <h1>Title</h1>
      • <input type=&quot;submit&quot; onClick=&quot;change();&quot; value=&quot;click!&quot;/>
      • </body>
      • </html>
      • <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
      • &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
      • <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
      • <head>
      • <title></title>
      • <meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; />
      • <script type=&quot;text/javascript&quot;>
      • //<![CDATA[
      • function change()
      • {
      • // Reference to the table - element
      • var table = document.getElementById(&quot;mytable&quot;);
      • var tr = document.createElement(&quot;tr&quot;); // <tr>
      • var td1 = document.createElement(&quot;td&quot;); // <td>
      • var td1Text = document.createTextNode(&quot;New Cell&quot;); // &quot;New Cell&quot;
      • td1.appendChild(td1Text); // <td>New Cell</td>
      • var td2 = document.createElement(&quot;td&quot;); // <td>
      • var td2Text = document.createTextNode(&quot;New Cell&quot;); // &quot;New Cell&quot;
      • td2.appendChild(td2Text); // <td>New Cell</td>
      • tr.appendChild(td1);
      • tr.appendChild(td2);
      • table.appendChild(tr);
      • }
      • //]]>
      • </script>
      • </head>
      • <body>
      • <table id=&quot;mytable&quot; border=&quot;1&quot;>
      • <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
      • <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
      • <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
      • </table>
      • <input type=&quot;submit&quot; onClick=&quot;change();&quot; value=&quot;Add Row&quot;/>
      • </body>
      • </html>
    SlideShare Zeitgeist 2009

    + Tampere University of Applied SciencesTampere University of Applied Sciences Nominate

    custom

    182 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 182
      • 182 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    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