23
Intro to jQuery Rafael Gonzaque Startup Institute July 28, 2014

Startup Institute Intro to jQuery (July 28, 2014)

Embed Size (px)

DESCRIPTION

This presentation covers why libraries like jQuery exist, what was web development like before jQuery, the main benefits of jQuery, and some alternate libraries.

Citation preview

Page 1: Startup Institute Intro to jQuery (July 28, 2014)

Intro to jQueryRafael GonzaqueStartup Institute

July 28, 2014

Page 2: Startup Institute Intro to jQuery (July 28, 2014)

Hi, I’m Rafael

• Software Engineer at Maker’s Row

• We connect brands and designers with manufacturers in the US

Page 3: Startup Institute Intro to jQuery (July 28, 2014)

About Maker’s Row• Launched in 2012 - http://makersrow.com

• 45K brands and 5K factories

• We provide 3 main abilities:

1. Projects

2. Search

3. Messaging

Page 4: Startup Institute Intro to jQuery (July 28, 2014)

• Like so many other sites, we use jQuery to make the development of our site’s interactivity easier

• Some other examples that use jQuery:

1. Airbnb

2. Foursquare

3. Tumblr (Ex: stives.tumblr.com)

• 78.5% of the top 10K sites (http://trends.builtwith.com/javascript/jQuery)

Page 5: Startup Institute Intro to jQuery (July 28, 2014)

Why are so many people using jQuery?

• CSS selectors

• A common interface to manipulate the DOM – the jQuery object

• A plugin system

Page 6: Startup Institute Intro to jQuery (July 28, 2014)

The DOM? Huh?• DOM = Document Object Model

• A programming interface for HTML, XML and SVG documents. It provides a structured representation of the document (a tree) and it defines a way that the structure can be accessed from programs so that they can change the document structure, style and content. (https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)

Page 7: Startup Institute Intro to jQuery (July 28, 2014)
Page 8: Startup Institute Intro to jQuery (July 28, 2014)

Accessing the DOM

• Core methods:

• document.getElementById

• element.getElementsByTagName

• document.createElement

https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction

Page 9: Startup Institute Intro to jQuery (July 28, 2014)

Example

<html><body>

<h1 id=“title”>My Title</h1><div class=“content”>

<p>My subheading</p><ul id=“task-list” class=“list”>

<li class=“item”><span class=“item-title”>Pickup

laundry</span><span class=“item-desc”>At the corner

laundromat</span></li>

</ul><a id=“ok-btn” class=“btn”>OK</a>

</div></body>

</html>

Page 10: Startup Institute Intro to jQuery (July 28, 2014)

Let’s make this interactive

var el = document.getElementById(‘ok-btn’);el.addEventListener(‘click’, function () {

// do something}, false);

Page 11: Startup Institute Intro to jQuery (July 28, 2014)

Uh no!

Page 12: Startup Institute Intro to jQuery (July 28, 2014)

X-Browser Issues• Until IE 11, IE didn’t implement

addEventListener; it had it’s own – attachEvent

• Solution:var el = document.getElementById(‘ok-btn’);if (IE < 11) {

el.attachEvent …} else {

el.addEventListener …}

• A bunch of x-browser issues between all the different vendors

Page 13: Startup Institute Intro to jQuery (July 28, 2014)

Enter jQuery

• Written by John Resig in 2006

• Provides DOM manipulation via its selector engine Sizzle (http://sizzlejs.com/)

Page 14: Startup Institute Intro to jQuery (July 28, 2014)

Same Example

<html><body>

<h1 id=“title”>My Title</h1><div class=“content”>

<p>My subheading</p><ul id=“task-list” class=“list”>

<li class=“item”><span class=“item-title”>Pickup

laundry</span><span class=“item-desc”>At the corner

laundromat</span></li>

</ul><a id=“ok-btn” class=“btn”>OK</a>

</div></body>

</html>

Page 15: Startup Institute Intro to jQuery (July 28, 2014)

$, FTW!• Change the color of the title

$(‘#title’).css(‘color’, ‘red’);

• Bind an event

$(‘#ok-btn’).on(‘click’, function () {

// do something

});

Notice there’s no need for checking IE! jQuery does it internally!

Page 16: Startup Institute Intro to jQuery (July 28, 2014)

More Examples• Add a new item to a list

$(‘#task-list’).append(‘<li class=“item”><span class=“item-title”>Get Milk</span><span class=“item-desc”>1% milkfat</span></li>’);

• Remove an item from the DOM

$(‘#task-list’).find(‘.item:last-child’).remove();

• Add a class to an element

$(‘.item’).eq(0).addClass(‘urgent’);

Page 17: Startup Institute Intro to jQuery (July 28, 2014)

Ajax before jQueryvar xhReq;

// Less than IE 7 supportif (window.ActiveXObject) {

xhReq = new ActiveXObject(‘Msxml2.XMLHTTP’);} else {

xhReq = new XmlHttpRequest();}

// Make an async requestxhReq.open(‘GET’, ‘my-app-endpoint?id=2’, true);xhReq.onreadystatechange = function() {

// We only care about complete requests (state = 4)if (xhReq.readyState != 4) { return; }

if (xhReq.status != 200) {// Handle error

} else {// Handle success via xhReq.responseText

}};xhReq.send(null);

Page 18: Startup Institute Intro to jQuery (July 28, 2014)

• No needing to deal with XMLHttpRequest for Ajax - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

• jQuery does ajax!$.ajax({

url: ‘/my-app-endpoint’,type: ‘GET’,data: { id: 2 },success: function (response) {

// Handle the response},error: function () {

// Handle the error}

});

• Full Documentation: http://api.jquery.com/

Ajax with jQuery

Page 19: Startup Institute Intro to jQuery (July 28, 2014)

jQuery Best Practices

• Prefix all variables that reference a jQuery object with a “$”var $taskList = $(‘#task-list’);

• A jQuery object is an array; check the length to see if an element existvar $taskList = $(‘#task-list),

$someElement = $(‘#parent .non-existing-selector’);

console.log($taskList.length); // 1console.log($someElement.length); // 0

Page 20: Startup Institute Intro to jQuery (July 28, 2014)

jQuery Best Practices• jQuery objects are chain-able

$(‘#title’).addClass(‘crucial’).text(‘Pay Attention’).on(‘click’, function () { /* Do something*/ });

• Use event delegation to bind events to multiple instances of the same html markup$(‘#task-list’).on(‘click’, ‘.item’, function () {

// Do something when a .item element is clicked});

• Always prefer CSS over Javascript; CSS is much faster

Page 21: Startup Institute Intro to jQuery (July 28, 2014)

Other DOM Libraries• Prototype.js - extends the native DOM and

provides id-based selection via $ and CSS selectors via $$ (http://prototypejs.org/)

• YUI - https://yuilibrary.com/

• Dojo - http://dojotoolkit.org/

• Zepto - http://zeptojs.com/ - just like jQuery, but smaller. (No IE support)

• MooTools - http://mootools.net/

Page 22: Startup Institute Intro to jQuery (July 28, 2014)

Wrap-Up• jQuery provides x-browser functionality for,

A. DOM manipulation

B. Ajax

• jQuery makes web development easier

• Always, prefer CSS solutions over Javascript with jQuery

• jQuery provides a common interface that make plugins possible. See:

1. http://plugins.jquery.com/

2. http://www.unheap.com/

3. http://learn.jquery.com/plugins/basic-plugin-creation/

Page 23: Startup Institute Intro to jQuery (July 28, 2014)

Rafael Gonzaque - @iamrgon

Thanks!