Chapter10 JavaScript3ExtendingJavaScriptwithjQuery
Chapter10 JavaScript3ExtendingJavaScriptwithjQuery
JavaScript
with jQuery
Chapter 10
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
A popular framework
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Including jQuery
<script src="http://code.jquery.com/jquery-3.1.0.min.js">
</script>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
jQuery Selector
Remember getElementByID()…
• The power of jQuery resides in the function named
jQuery().
• There’s also an alias for this function named $() .
• You can combine CSS selectors with the $()
notation to select DOM objects that match CSS
attributes
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
jQuery Selector
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Basic Selectors
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Some examples
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Advanced Selectors (Go back to Chapter 4 in CSS for a refresher)
• Attribute Selector
• Pseudo-Element Selector
• Contextual Selector
• jQuery Filters
• Form Selectors
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
jQuery's content filter selection
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Common element Manipulations - HTML attributes
• We can both set and get an attribute value by using the attr()
method.
// link is assigned the href attribute of the first <a> tag
$("a").attr("href","http://funwebdev.com");
$("img").attr("class","fancy");
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Common element Manipulations - HTML properties
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
jQuery Foundations
Common element Manipulations – Changing CSS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Event Handling in jQuery
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Event Handling in jQuery
Binding and Unbinding Events
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Event Handling in jQuery
Page Loading
$(document).ready(function() {
// set up listeners knowing page loads before this runs
$("#example").click(function () {
$("#message").html("you clicked");
});
});
Or the even simpler
$(function () {
...
});
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Creating Nodes
// jQuery version 1
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Creating Nodes
// jQuery version 2
var link2 = $('<a></a>');
link2.attr("href","http://funwebdev.com");
link2.attr("title","jQuery verbose");
link2.html("Visit Us");
// version 3
$('<a>', {
href: 'http://funwebdev.com',
title: 'jQuery',
text: 'Visit Us'
});
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Adding DOM Elements
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Wrapping Existing DOM in New Tags
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Wrapping Existing DOM in New Tags
<div class="external-links">
<div class="gallery">Uffuzi Museum</div>
<div class="gallery">National Gallery</div>
<div class="link-out">funwebdev.com</div>
</div>
$(".gallery").wrap('<div class="galleryLink"><div>');
<div class="external-links">
<div class="galleryLink">
<div class="gallery">Uffuzi Museum</div>
</div>
<div class="galleryLink">
<div class="gallery">National Gallery</div>
</div>
<div class="link-out">funwebdev.com</div>
</div>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
DOM Manipulation
Wrapping Existing DOM in New Tags
<div class="external-links">
<div class="gallery">Uffuzi Museum</div>
<div class="gallery">National Gallery</div>
<div class="link-out">funwebdev.com</div>
</div>
$(".gallery").wrap(function() {
return "<div class='galleryLink' title='Visit " + $(this).html() + "'></div>";
});
<div class="external-links">
<div class="galleryLink" title="Visit Uffuzi Museum">
<div class="gallery">Uffuzi Museum</div>
</div>
<div class="galleryLink" title="Visit National Gallery">
<div class="gallery">National Gallery</div>
</div>
<div class="link-out">funwebdev.com</div>
</div>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Show() and fadeIn()
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Using slide()
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Raw Animation
$("#box").animate({left: '495px'});
• Describes a final state in CSS.
• The state before defines where the animations
starts.
• Animate() has many parameters including:
• Duration
• Step
• Done
• …
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Raw Animation
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Easing Functions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Effects and Animation
Easing Functions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Asynchronous JavaScript with XML (AJAX)
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Making Asynchronous Requests
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Synchronous example
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Asynchronous example – what's changed?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Making Asynchronous requests – load()
$("#timeDiv").load("currentTime.php");
• Asynchronously calls currentTime.php and puts the
returned content into the selected div with id
timeDiv
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Making Asynchronous requests - GET
$.get("serviceTravelCountries.php?name=Italy");
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Making Asynchronous requests – GET formal
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
The jqXHR Object
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
The jqXHR Object
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
The POST Method
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
The POST Method – form serialization
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Complete Control over AJAX
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
AJAX
Cross-Origin Resource Sharing
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Asynchronous File Transmission
The FormData Interface
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Asynchronous File Transmission
The FormData Interface
function uploadFile () {
// get the file as a string
var formData = new FormData($("#fileUpload")[0]);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", transferComplete, false);
xhr.addEventListener("error", transferFailed, false);
xhr.addEventListener("abort", transferCanceled, false);
xhr.open('POST', 'upload.php', true);
xhr.send(formData); // actually send the form data
function transferComplete(evt) { // stylized upload complete
$("#progress").css("width","100%");
$("#progress").html("100%");
}
function transferFailed(evt) {
alert("An error occurred while transferring the file.");
}
function transferCanceled(evt) {
alert("The transfer has been canceled by the user.");
}
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Asynchronous File Transmission
Appending Files to a POST
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 10
1 jQuery
Foundations 2 Event Handling
in jQuery
3 DOM
Manipulation 4 Effects and
Animation
Asynchronous
5 AJAX
6 File
Transmission
7
Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Key Terms
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Questions?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.