C5 Javascript
Upcoming SlideShare
Loading in...5
×
 

C5 Javascript

on

  • 4,926 views

 

Statistics

Views

Total Views
4,926
Views on SlideShare
2,520
Embed Views
2,406

Actions

Likes
0
Downloads
59
Comments
0

1 Embed 2,406

http://ipwfils.wordpress.com 2406

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

C5 Javascript C5 Javascript Presentation Transcript

  • Client-side Scripting Languages Introduction to Javascript
  • Plan of the course
    • Javascript – Why, When, What
    • How to include Javascript in HTML
    • Javascript syntax
    • Document Object Model
    • Examples
  • Javascript – Why, When, What
    • At first we had only simple text + links + images pages
    • “ live” pages were required by the market
    • Netscape invented “LiveScript” in 1995
    • Later renamed to Javascript
    • Javascript – run on browsers, access dynamically to the html page
    • The language was (is) interpreted usually in different way by the browsers
  • Include Javascript into HTML
    • Script inclus in pagina html
      • <script type=”text/javascript”>
      • //cod script
      • </script>
    • Script inclus intr-un fisier exterior
      • <script src=”fisier_sursa.js”></script>
  • When does the code run
    • If the script tag is included in the <head> tag
      • The script is executed when the page is loaded – before actually rendering it
    • If the script is included in the body page
      • The script is executed when the markup is found
  • Javascript Syntax
    • Very similar with C & Java
    • No types for variables
      • Declare variables using the term var
      • var x=5, y=7;
    • Functions are declared using the function keyword
      • function sum(x,y)
      • { var rez=x+y; return rez;}
    • Calling functions is the same as in C/Java
      • var suma=sum(2,5);
  • Javascript objects
    • Objects have
      • methods (functions)
      • Properties
    • Example
      • var student={nume: &quot;ion&quot;, an:2, note:{mate:9, pc:8}};
      • alert(student.nume +&quot;<br>&quot; );
      • alert(student.an +&quot;<br>&quot;);
      • var nota
      • var student={nume: &quot;ion&quot;, an:2, note:{mate:9, pc:8}};
  • Javascript predefined objects
    • Math
      • http://www.w3schools.com/jsref/jsref_obj_math.asp
    • String
      • http://www.w3schools.com/jsref/jsref_obj_string.asp
    • Array
      • http://www.w3schools.com/jsref/jsref_obj_array.asp
    • Date
      • http://www.w3schools.com/jsref/jsref_obj_date.asp
  • Examples
    • <script type=&quot;text/javascript&quot;>
    • function printValue() //declare a function
    • {
    • var x=Math.random()*10; //compute the value of x as a random value between 0 and 10
    • alert(x); //display an alert containing the value of x
    • var y=&quot;a random text&quot;; //create a variable of type string
    • alert(y.length); //display an alert containing the length of y
    • //!!! length is a property and not a method
    • alert(y.substr(0,5)); //compute and display as an alert the substring of y between the first and the 5 th character
    • alert(x+&quot; &quot;+y.length+“ “+y.substr(5,y.length));
    • //display as an alert the string formed by x, length of y and the substring formed from the 5 th character of y until the last one
    • }
    • </script>
  • Javascript example - followup
    • String concatenation operator “+”
    • The alert is used for displaying information during development. NOT to be used in applications
    • Objects can have methods like y.substr(0,5) and properties like y.length
    • All types of variables are declared using var
  • Events
    • HTML elements can detect when the user interacts with them
    • Examples of interactions
      • Mouse over (mouse out)
      • Click
      • Key pressed
      • Blur
      • change
    • We can add javascript code to handle every interaction
  • Events examples
    • <script type=“text/javascript”>
    • function youClicked(element)
    • {this.innerHTML=&quot;you clicked this element&quot;;}
    • function youMousedOver()
    • {alert(&quot;mouse over detected&quot;); }
    • </script>
    • <h1 onclick=&quot;alert('youclicked');youClicked( this );&quot; onmouseover=&quot;youMousedOver()&quot;> Introduction dans la programmation web</h1>
  • Javascript Events
    • Why we need and what is the “this” pointer
    Html element User interacts event1 event2 Event1 associated javascript function In order for this function to be able to modify the element with which the user interacted we need to pass it a reference to the object “this” is the reference to the object on which the event is handled
  • DOM
    • DOM=Document Object Model
    • DOM = a way to access the elements of a HTML page
  • DOM
    • The DOM tree contains nodes which can be
      • Html elements
      • Text
    • The tree elements can be accessed
      • By traversing the tree (See Data structures course)
      • By accessing them directly by name (getElementsByTagName)
      • By accessing them directly by id (getElementById)
      • Addressing them directly (as in an array)
    • The root of the DOM tree is the document
  • Methods and properties
    • document.write(“text”)
      • Adds the “text” to the given page
      • If the page is finished loading it rewrites it
      • Example
      • <script type=&quot;text/javascript&quot;>
        • function printValue()
        • {
        • var x=Math.random()*10;
        • alert(x);
        • var y=&quot;a random text&quot;;
        • alert(y.length);
        • alert(y.substr(0,5));
        • alert(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length));
        • document.write(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length));
        • }
        • </script>
  • DOM Methods and properties
    • getElementsByTagName
      • Returns an array of elements with a given name
      • The we need to know the position of the element we need to modify inside the array
      • function modifySecondH1()
      • {
      • var headersArray=document.getElementsByTagName(&quot;h1&quot;); //retrieves all the elements whose names are h1
      • //elements’ numbering in the array starts at 0
      • headersArray[1].innerHTML=Math.random()*5; }
  • DOM Methods and properties
    • document.getElementById
      • The most used method to access an element of a html page
      • We have to be careful to set ids for the elements
      • function modifyFirstH1()
      • {
      • //retrieve the element with the id h1id1
      • var h1Element=document.getElementById(&quot;h1id1&quot;);
      • //set the HTML value for the document
      • h1Element.innerHTML=&quot;new title&quot;;
      • }
  • DOM objects methods and properties
    • Direct access to the element
    • Predefined collections
      • Forms
      • Links
      • Images
    • document.forms[0].username.value – accesses the first form in the document and sets the value property for the username input
  • Example – using javascript to validate forms
    • When a form is submitted we need to validate it first on the client-side
    • The form should be validated before submitting
    • The event should be added to the submit button
    • For example we want to check if 2 passwords have the same value and if the username is longer than 4 characters
  • Validate forms with Javascript – example (I)
    • function validateForm(){
    • var usernameElement=document.getElementById(&quot;username&quot;);
    • var passwordElement=document.getElementById(&quot;password&quot;);
    • var rePasswordElement=document.getElementById(&quot;repassword&quot;);
    • if(passwordElement.value!=rePasswordElement.value || passwordElement.value.length==0)
    • {alert(&quot;please make sure the password is entered the same twice&quot;);return;}
    • if (usernameElement.value.length<4)
    • {alert(&quot;please make sure the username is longer than 4 letters&quot;);return; }
    • document.forms[0].submit();
    • }
  • Example of form validation with Javascript (II)
    • <form action=&quot;script.php&quot; method=&quot;POST&quot;>
    • nom d'utilisateur<input type=&quot;text&quot; id=&quot;username&quot; /><br/>
    • mot de passe<input type=&quot;password&quot; id=&quot;password&quot; /> <br/>
    • mot de passe encore une fois <input id=&quot;repassword&quot; type=&quot;password&quot;> <br/>
    • <input type=&quot;button&quot; value=&quot;send&quot; onclick=&quot;validateForm();&quot;/>
    • </form>
  • Javascript debugging
    • Firebug – extension for Firefox
      • Allows debugging of scripts
      • Step by step execution
      • Adding breakpoints
      • Watch expressions
      • Visualize the DOM tree
  • Javascript debugging example