Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Dynamic Webpages

Similar presentations


Presentation on theme: "Creating Dynamic Webpages"— Presentation transcript:

1 Creating Dynamic Webpages
JavaScript & jQuery Creating Dynamic Webpages

2 About jQuery “Write less, do more.” MIT License
Great cross-platform support (1.x more widely supported than 2.x)

3 Browser Support jQuery jQuery UI

4 Interacting with the Page
Selectors and Accessors

5 The jQuery syntax is made for selecting HTML elements and performing some action on the elements.
Basic syntax is: $(selector).action() A $ sign to define/access jQuery A (selector) to "query (or find)" HTML elements A jQuery action() to be performed on the element(s)

6 Selectors Selectors are used to tell JavaScript & jQuery which element(s) you are interested in working with Selectors follow the same pattern for jQuery as for CSS

7 Examples: $(this).hide() - hides the current element
$("p").hide() - hides all <p> elements $(".test").hide() - hides all elements with class=“test” $("#test").hide() - hides the element with id=“test”

8 What Can jQuery do For You?
JavaScript JQuery Cumbersome syntax Returns DOM objects Minimal code required Returns jQuery object (wraps DOM and adds functionality & convenience)

9 JS vs jQuery JavaScript jQuery
<div class="interactive" id="body"> Body Content </div> #body .interactive <div class="interactive" id="footer"> Footer Content </div> #footer .interactive JavaScript jQuery //Returns the body div document.getElementById("body"); //Returns both divs (both participate in the "interactive" class) document.getElementsByClassName("interactive"); //Returns the body div $("#body"); //Returns both divs (both participate in the "interactive" class) $(".interactive");

10 JavaScript Selection Methods
document.getElementById("myID");. document.getElementsByClassName("myName"); document.getElementsByName("myName"); document.getElementsByTagName("div"); document.querySelector(“#id"); document.querySelectorAll(“.class");

11 jQuery Selection Methods
$("Any pattern and/or combination of selectors you feel like");

12 Accessors JavaScript jQuery Get an object’s HTML
var html = document.getElementById(“myElement”).inner HTML; Set an object’s HTML document.getElementById(“#myElement”).inne rHTML = “<span> Display this HTML </span>"; Get an object’s value var text = document.getElementById(“myElement”).value ; Set an object’s value document.getElementById(“myElement”).value = "Display this string"; Get an object’s HTML var html = $(“#myElement”).html(); Set an object’s HTML $(“#myElement”).html(“<span> Display this HTML</span>"); Get an object’s text var text = $(“#myElement”).text(); Set an object’s text $(“#myElement”).text("Display this string"); Get an object’s value var value = $(“#myElement”).val(); Set an object’s value $(“#myElement”).val("Display this string");

13 $().text(); From jQuery

14 Document Ready Event $(document).ready(function(){    // jQuery methods go here });

15 Example: $(document).ready(function(){     $("button").on(“click”,function(){         $("p").hide();     }); }); Mouse events - .click, .dblclick, .mouseenter, ….. Keyboard Events - .keypress, .keydown…….

16 <!DOCTYPE html> <html> <head> <script src=" </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

17 jQuery prototyping All objects have a prototype property. It is simply an object from which other objects can inherit properties.

18 Example function Person(name) { this.name = name; }
Person.prototype.sayHello = function () { alert(this.name + " says hello"); }; var james = new Person("James"); james.sayHello(); // Alerts "James says hello“

19 Explanation of Example
In this example, Person is a constructor function. It can be instantiated by calling it with the new operator. Inside the constructor, the this keyword refers to the instance, so every instance has its own name property. The prototype of Person is shared between all instances. So all instances of Person have a sayHello method that they inherit from Person.prototype. By defining the sayHello method as a property of Person.prototype we are saving memory. We could just as easily give every instance of Person its own copy of the method (by assigning it to this.sayHello inside the constructor), but that's not as efficient.

20 Method chaining Method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results jQuery relies heavily on chaining. This makes it easy to call several methods on the same selection

21 Java Public class DiaLog{ public DiaLog setTitle(String title){ //logic set title return this; } public DiaLog setMessage(String msg){ //logic set msg new Dialog().setTitle(“Title”).setMessage(“message”)

22 jQuery $(document). ready( function() { $(“li”). css(“color”,”blue”)
jQuery $(document).ready( function() { $(“li”).css(“color”,”blue”) .slideUp(1000) .slideDown(1000) .attr(“title”,”my title”); } );

23 AJAX Asynchronous JavaScript and xml

24 What's AJAX? Group of web development techniques on the client side that enable the update of parts of page dynamically rather than requesting a whole page every time an event is triggered. Asynchronous means that your page can update any part without having to send an entire request for the whole page. JavaScript does the heavy lifting (handling events, server request and updating the page). uses XHR API for client server communication. It’s called AJAX, but the input format can be text, html, or json.

25 Why use AJAX More efficient way to transfer data.
Less time spent on updating. Better user experience. Page is more dynamic and responsive.

26 Example <!DOCTYPE html> <html> <head> <script> function loadXMLDoc(url) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() if (xmlhttp.readyState==4 && xmlhttp.status==200) document.getElementById('A1').innerHTML=xmlhttp.status; document.getElementById('A2').innerHTML=xmlhttp.statusText; document.getElementById('A3').innerHTML=xmlhttp.responseText; } xmlhttp.open("GET",url,true); xmlhttp.send(); </script> </head> <body> <h2>Retrieve data from XML file</h2> <p><b>Status:</b><span id="A1"></span></p> <p><b>Status text:</b><span id="A2"></span></p> <p><b>Response:</b><span id="A3"></span></p> <button onclick="loadXMLDoc('note.xml')">Get XML data</button> </body> </html>

27 JSON Objects A Brief Glossing

28 What is a JSON Object? A JSON object is an associative array (map/dictionary) that is machine and (arguably) human readable Commonly contains nested arrays/objects

29 What Does it Look Like? {"employees":[     {"firstName":"John", "lastName":"Doe"},     {"firstName":"Anna", "lastName":"Smith"},     {"firstName":"Peter", "lastName":"Jones"} ]}

30 References & Resources
prototype-mean-here-in-the-jquery-source-code


Download ppt "Creating Dynamic Webpages"

Similar presentations


Ads by Google