SlideShare a Scribd company logo
1 of 33
LESSON FOUR
    jQuery intro
Fast Review
Functions = “First Class Objects”
Anonymous Functions

     function() {
      // code goes here
      }
Arrays


data structure for ordered values
   var team = [‘ben’,‘jeff’,‘kam’];
   team[0] // => ‘ben’
Objects



collections of “key/value” properties
  var person = {};
  person.name = ‘will’;    // Write
  var name = person.name; // Read
Document Object Model
Review Homework
Enter, jQuery
What is jQuery

Framework for using Javascript to interact with
the browser
•   Simple DOM API: manipulate HTML / CSS on the page

•   Simple AJAX API: send and receive data from the server

•   Simple Event API: do stuff when the user does stuff

•   Thats it!
What is a Framework?

                    A library of utility functions

Small                  Medium                   Large
•   Underscore.js       •   jQuery UI       •    YUI (Yahoo UI)
•   jQuery              •   MooTools        •    GWT (Google web toolkit)
•   Prototype.js        •   Dojo            •    EXT JS (commercial)



      there are LOTS of them for web development
Why use jQuery
•   The DOM API is _nasty_!

•   Cross browser

•   Fast - Faster than raw DOM manipulation! *magic*

•   Lightweight (31kb)

•   Stable and trusted

•   Required for many other frameworks

•   EVERYBODY uses it
Include jQuery
<script src="/js/jquery.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/
jquery.min.js"></script>



                         Try it out!
jQuery Syntax
  or…how to follow the $

$(document).ready(function(){
  // Your code here
});




    DOM Manipulation
   $("a").addClass("test");
using jQuery
Selecting Elements
                      Using CSS!


$("#foo")     // the [element] at id “foo”


$(".bloop")   // the [elements] with class “bloop”


$("a.buzz")   // the [links] with class ‘buzz’



$(".bloop:first")      // specialty! The first bloop!


=>   :hidden, :visible, :last, :checked, :empty,       :odd...
Important Methods
.show() / .hide() / .toggle()
   $(".swap_text").toggle();


.addClass(".active")
.removeClass(".active")


.css(key, value)
   $("#my_box").css(‘backgroundColor’,   ‘red’);
   $("#my_box").css({

            ‘backgroundColor’ : ‘red’,
            ‘color’ : ‘#030’
   });
Important Methods

.animate(properties, duration, callback)

   $(‘#my_box’).animate({
            ‘backgroundColor’ : ‘red’,
            ‘marginTop‘         : ‘20px’,
            ‘color‘             : ‘#030’
   }, 3000, justFinished);   // execute the animation for 3 seconds
                             // and when done, call: justFinished();
Important Methods

.html() / .val() / .attr(‘attribute’)

   $(‘#my_title’).html(); // returns the innerHTML

   $(‘#my_title’).html(“New Title!”); // sets the innerHTML



   $(‘input#email’).val(“please enter your email address...”);



   $(‘img’).attr(“src”, “placekitten.com/30/30”);



                             as before: these methods can get or set...
Methods Can “Chain”




$("#my_box").addClass(‘inactive’).removeClass(‘active’).hide();
jQuery Events

$(document).ready()     // the DOM is loaded
window.load()           // all elements (including images)
                           have loaded


.change()
.submit()
.focus()
.click
.scroll()
.hover()
.keypress()                                ...And so many more!
Events w/o jQuery
                      no browser consistency




EX:


.addEventListener("click", function({}) );   // Firefox


.attachEvent("onclick", function({}) );      // Internet Explorer
jQuery Insertion




.append()
.after()
.insertAfter()
.before
.insertBefore()
.clone()
Client Side
   Requests
[XHR and AJAX]
XMLHttpRequest




Allows you to make a web request via client side javascript
Invented by Microsoft for Internet Explorer, then ported to other
browsers

   var request = new XMLHttpRequest(); // Create new request object
   request.open(“GET”, “path/file.ext”, false); // “open” the request
   request.send(null); // Make the request
   var text = request.responseText; // Check the response text
BUZZZZWORD




“XHR”
•   XMLHttpRequest

•   Misnomer => Not limited to XML data

•   By default, synchronous
AJAX




By default, XHR proceeds synchronously
In other words, your code will hang until the request is complete
What if you want your code to keep executing?
   => Use AJAX
BUZZZZWORD



“AJAX”
•   “Asynchronous Javascript and XML”

•   XHR done asynchronously

•   Provide a callback to execute when the request is complete

•   Changed the face of web development => page reloads no longer
    required to add new content to a page
AJAX via DOM



var request = new XMLHttpRequest(); // Create new request object
request.open(“GET”, “path/file.ext”, true); // ‘True’ => AJAX

request.send(null); // Make the request
var text = request.responseText; // Will be null this time!
request.onreadystatechange = function() {
 if (request.readyState == 4) {
     // Request is done. Can check responseText
 }
AJAX via DOM



var request = new XMLHttpRequest(); // Create new request object
request.open(“GET”, “path/file.ext”, true); // ‘True’ => AJAX


          CONFUSING this time!
request.send(null); // Make the request
var text = request.responseText; // Will be null
request.onreadystatechange = function() {
 if (request.readyState == 4) {
     // Request is done. Can check responseText
 }
AJAX via jQuery



    $.ajax({
  url: "test.html",
  success: function(return_text){
    $("#results").append(return_text);
 }
});




                          more next week...
QUESTIONS?
contact will_and_bay@hackyale.com

More Related Content

What's hot

Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQuery
Angel Ruiz
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
brinsknaps
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 

What's hot (20)

jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
jQuery
jQueryjQuery
jQuery
 
jQuery
jQueryjQuery
jQuery
 
jQuery Features to Avoid
jQuery Features to AvoidjQuery Features to Avoid
jQuery Features to Avoid
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and Bling
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQuery
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
 
D3.js and SVG
D3.js and SVGD3.js and SVG
D3.js and SVG
 
Testable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScriptTestable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScript
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
Don't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQueryDon't Worry jQuery is very Easy:Learning Tips For jQuery
Don't Worry jQuery is very Easy:Learning Tips For jQuery
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 

Viewers also liked (6)

Metodología de Investigación "Comunicative Daile Life Stories"
Metodología de Investigación "Comunicative Daile Life Stories"Metodología de Investigación "Comunicative Daile Life Stories"
Metodología de Investigación "Comunicative Daile Life Stories"
 
Application 1785686
Application 1785686Application 1785686
Application 1785686
 
2007 bpc conference
2007 bpc conference2007 bpc conference
2007 bpc conference
 
Lives of the Great War: Building First World War life stories across archives...
Lives of the Great War: Building First World War life stories across archives...Lives of the Great War: Building First World War life stories across archives...
Lives of the Great War: Building First World War life stories across archives...
 
Alexis Willey
Alexis Willey Alexis Willey
Alexis Willey
 
State of the LexBlog Address 2016 - Slides from LexBlog's Webinar
State of the LexBlog Address 2016 - Slides from LexBlog's WebinarState of the LexBlog Address 2016 - Slides from LexBlog's Webinar
State of the LexBlog Address 2016 - Slides from LexBlog's Webinar
 

Similar to Week 4 - jQuery + Ajax

Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
Hjörtur Hilmarsson
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
Sven Haiges
 

Similar to Week 4 - jQuery + Ajax (20)

A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 
jQuery
jQueryjQuery
jQuery
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NET
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
JavaScript JQUERY AJAX
JavaScript JQUERY AJAXJavaScript JQUERY AJAX
JavaScript JQUERY AJAX
 
J query training
J query trainingJ query training
J query training
 
jQuery for web development
jQuery for web developmentjQuery for web development
jQuery for web development
 
jQuery: Events, Animation, Ajax
jQuery: Events, Animation, AjaxjQuery: Events, Animation, Ajax
jQuery: Events, Animation, Ajax
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
 
22 j query1
22 j query122 j query1
22 j query1
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
JQuery UI
JQuery UIJQuery UI
JQuery UI
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Week 4 - jQuery + Ajax

  • 1.
  • 2. LESSON FOUR jQuery intro
  • 4. Functions = “First Class Objects”
  • 5. Anonymous Functions function() { // code goes here }
  • 6. Arrays data structure for ordered values var team = [‘ben’,‘jeff’,‘kam’]; team[0] // => ‘ben’
  • 7. Objects collections of “key/value” properties var person = {}; person.name = ‘will’; // Write var name = person.name; // Read
  • 11. What is jQuery Framework for using Javascript to interact with the browser • Simple DOM API: manipulate HTML / CSS on the page • Simple AJAX API: send and receive data from the server • Simple Event API: do stuff when the user does stuff • Thats it!
  • 12. What is a Framework? A library of utility functions Small Medium Large • Underscore.js • jQuery UI • YUI (Yahoo UI) • jQuery • MooTools • GWT (Google web toolkit) • Prototype.js • Dojo • EXT JS (commercial) there are LOTS of them for web development
  • 13. Why use jQuery • The DOM API is _nasty_! • Cross browser • Fast - Faster than raw DOM manipulation! *magic* • Lightweight (31kb) • Stable and trusted • Required for many other frameworks • EVERYBODY uses it
  • 14. Include jQuery <script src="/js/jquery.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/ jquery.min.js"></script> Try it out!
  • 15. jQuery Syntax or…how to follow the $ $(document).ready(function(){ // Your code here }); DOM Manipulation $("a").addClass("test");
  • 17. Selecting Elements Using CSS! $("#foo") // the [element] at id “foo” $(".bloop") // the [elements] with class “bloop” $("a.buzz") // the [links] with class ‘buzz’ $(".bloop:first") // specialty! The first bloop! => :hidden, :visible, :last, :checked, :empty, :odd...
  • 18. Important Methods .show() / .hide() / .toggle() $(".swap_text").toggle(); .addClass(".active") .removeClass(".active") .css(key, value) $("#my_box").css(‘backgroundColor’, ‘red’); $("#my_box").css({ ‘backgroundColor’ : ‘red’, ‘color’ : ‘#030’ });
  • 19. Important Methods .animate(properties, duration, callback) $(‘#my_box’).animate({ ‘backgroundColor’ : ‘red’, ‘marginTop‘ : ‘20px’, ‘color‘ : ‘#030’ }, 3000, justFinished); // execute the animation for 3 seconds // and when done, call: justFinished();
  • 20. Important Methods .html() / .val() / .attr(‘attribute’) $(‘#my_title’).html(); // returns the innerHTML $(‘#my_title’).html(“New Title!”); // sets the innerHTML $(‘input#email’).val(“please enter your email address...”); $(‘img’).attr(“src”, “placekitten.com/30/30”); as before: these methods can get or set...
  • 22. jQuery Events $(document).ready() // the DOM is loaded window.load() // all elements (including images) have loaded .change() .submit() .focus() .click .scroll() .hover() .keypress() ...And so many more!
  • 23. Events w/o jQuery no browser consistency EX: .addEventListener("click", function({}) ); // Firefox .attachEvent("onclick", function({}) ); // Internet Explorer
  • 25. Client Side Requests [XHR and AJAX]
  • 26. XMLHttpRequest Allows you to make a web request via client side javascript Invented by Microsoft for Internet Explorer, then ported to other browsers var request = new XMLHttpRequest(); // Create new request object request.open(“GET”, “path/file.ext”, false); // “open” the request request.send(null); // Make the request var text = request.responseText; // Check the response text
  • 27. BUZZZZWORD “XHR” • XMLHttpRequest • Misnomer => Not limited to XML data • By default, synchronous
  • 28. AJAX By default, XHR proceeds synchronously In other words, your code will hang until the request is complete What if you want your code to keep executing? => Use AJAX
  • 29. BUZZZZWORD “AJAX” • “Asynchronous Javascript and XML” • XHR done asynchronously • Provide a callback to execute when the request is complete • Changed the face of web development => page reloads no longer required to add new content to a page
  • 30. AJAX via DOM var request = new XMLHttpRequest(); // Create new request object request.open(“GET”, “path/file.ext”, true); // ‘True’ => AJAX request.send(null); // Make the request var text = request.responseText; // Will be null this time! request.onreadystatechange = function() { if (request.readyState == 4) { // Request is done. Can check responseText }
  • 31. AJAX via DOM var request = new XMLHttpRequest(); // Create new request object request.open(“GET”, “path/file.ext”, true); // ‘True’ => AJAX CONFUSING this time! request.send(null); // Make the request var text = request.responseText; // Will be null request.onreadystatechange = function() { if (request.readyState == 4) { // Request is done. Can check responseText }
  • 32. AJAX via jQuery $.ajax({   url: "test.html",   success: function(return_text){     $("#results").append(return_text);  } }); more next week...

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n