SlideShare a Scribd company logo
1 of 20
JQUERY BEST PRACTICE
AND
SELECTORS
Author: Chandra Shekher P
© chandrashekher
TOPICS
1. Loading jQuery
2. Variables
3. Selectors
4. Dom Manipulation
5. Events
6. Ajax
7. Animations
8. Plugins
9. Chaining
10. Miscellaneous
LOADING JQUERY
 Always try to use a CDN to include jQuery on your page,
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script> <script>window.jQuery || document.write('<script src="js/jquery-2.1.1.min.js"
type="text/javascript"></script>')</script>
 If possible, keep all your JavaScript and jQuery includes at the bottom
of your page.
 For advanced browser feature detection, use Modernizr.js .
 If you are using other libraries like Prototype, MooTools, Zepto etc. that
uses $ sign as well,
 try not to use $ for calling jQuery functions and instead
use jQuery simply. You can return control of $ back to the other library
with a call to $.noConflict().
JQUERY VARIABLES
 All variables that are used to store/cache jQuery
objects should have a name prefixed with a $.
 Always cache your jQuery selector returned objects
in variables for reuse.
var $myDiv = $("#myDiv");
$myDiv.click(function(){...});
 Use camel case for naming variables.
SELECTORS
 Use ID selector whenever possible. It is faster because they are handled
using document.getElementById().
 When using class selectors, don't use the element type in your selector. for
Performance Improvements.
var $products = $("div.products"); // SLOW
var $products = $(".products"); // FAST
 Use find for Id->Child nested selectors. The .find() approach is faster because the first
selection is handled without going through the Sizzle selector engine.
// BAD, a nested query for Sizzle selector engine
var $productIds = $("#products div.id");
// GOOD, #products is already selected by document.getElementById() so only div.id
needs
to go through Sizzle selector engine
var $productIds = $("#products").find("div.id");
SELECTORS
 Be specific on the right-hand side of your selector, and less
specific on the left
// Unoptimized $("div.data .gonzalez"); //
Optimized $(".data td.gonzalez");
 Avoid Excessive Specificity
$(".data table.attendees td.gonzalez"); // Better: Drop the middle if
possible.
$(".data td.gonzalez");
SELECTORS
 Give your Selectors a Context.
// SLOWER because it has to traverse the whole DOM for
.class
$('.class');
// FASTER because now it only looks under class-container.
$('.class', '#class-container');
 Avoid Universal Selectors
$('div.container > *'); // BAD
$('div.container').children(); // BETTER
SELECTORS
 Avoid Implied Universal Selectors. When you leave off the
selector, the universal selector (*) is still implied.
$('div.someclass :radio'); // BAD
$('div.someclass input:radio'); // GOOD
Don’t Descend Multiple IDs or nest when selecting an ID. ID-only
selections are handled using document.getElementById() so
don't mix them with other selectors.
$('#outer #inner'); // BAD
$('div#inner'); // BAD
$('.outer-container #inner'); // BAD
$('#inner'); // GOOD, only calls
document.getElementById()
DOM MANIPULATION
 Always detach any existing element before manipulation and
attach it back after manipulating it.
var $myList = $("#list-container > ul").detach();
//...a lot of complicated things on $myList
$myList.appendTo("#list-container");
 Don’t Act on Absent Elements.
// BAD: This runs three functions before it realizes there's nothing in the
selection
$("#nosuchthing").slideUp();
// GOOD
var $mySelection = $("#nosuchthing");
if ($mySelection.length) {
$mySelection.slideUp();
}
DOM MANIPULATION
 Use string concatenation or array.join() over .append()
// BAD
var $myList = $("#list");
for(var i = 0; i < 10000; i++){
$myList.append("<li>"+i+"</li>");
}
// GOOD var $myList = $("#list");
var list = "";
for(var i = 0; i < 10000; i++){
list += "<li>"+i+"</li>";
}
$myList.html(list);
// EVEN FASTER
var array = [];
for(var i = 0; i < 10000; i++){
array[i] = "<li>"+i+"</li>";
}
$myList.html(array.join(''));
EVENTS
 Use only one Document Ready handler per page. It makes it easier
to debug and keep track of the behavior flow.
 DO NOT use anonymous functions to attach events. Anonymous
functions are difficult to debug, maintain, test, or reuse.
$("#myLink").on("click", function(){...}); // BAD
// GOOD
function myLinkClickHandler(){...}
$("#myLink").on("click", myLinkClickHandler);
 Document ready event handler should not be an anonymous
function. Once again, anonymous functions are difficult to debug,
maintain, test, or reuse.
$(function(){ ... }); // BAD: You can never reuse or write a test for this function.
// GOOD
$(initPage); // or $(document).ready(initPage);
function initPage(){
// Page load event where you can initialize values and call other
initializers.
EVENTS
 Document ready event handlers should be included from
external files and inline JavaScript can be used to call the
ready handle after any initial setup.
<script src="my-document-ready.js"></script>
<script>
// Any global variable set-up that might be needed.
$(document).ready(initPage); // or
$(initPage);
</script>
 DO NOT use behavioral markup in HTML (JavaScript inlining),
these are debugging nightmares. Always bind events with
jQuery to be consistent so it's easier to attach and remove
events dynamically.
<a id="myLink" href="#" onclick="myEventHandler();"> my link
</a> <!-- BAD -->
$("#myLink").on("click", myEventHandler); // GOOD
EVENTS
 When possible, use custom namespace for events. It's easier to
unbind the exact event that you attached without affecting other
events bound to the DOM element.
$("#myLink").on("click.mySpecialClick", myEventHandler); // GOOD
// Later on, it's easier to unbind just your click event
$("#myLink").unbind("click.mySpecialClick");
 Use event delegation when you have to attach same event to
multiple elements. Event delegation allows us to attach a single
event listener, to a parent element, that will fire for all
descendants matching a selector, whether those descendants
exist now or are added in the future.
$("#list a").on("click", myClickHandler); // BAD, you are attaching an
event to all
the links under the list.
$("#list").on("click", "a", myClickHandler); // GOOD, only one event
handler is
attached to the parent.
AJAX
 Avoid using .getJson() or .get(), simply use the $.ajax() as
that's what gets called internally.
 DO NOT use http requests on https sites. Prefer schemaless
URLs (leave the protocol http/https out of your URL)
 DO NOT put request parameters in the URL, send them using
data object setting.
// Less readable...
$.ajax({
url: "something.php?param1=test1&param2=test2", ....
});
// More readable...
$.ajax({
url: "something.php",
data: { param1: test1, param2: test2 }
});
AJAX
 Try to specify the dataType setting so it's easier to know what
kind of data you are working with.
 Use Delegated event handlers for attaching events to content
loaded using Ajax. Delegated events have the advantage that
they can process events from descendant elements that are
added to the document at a later time
$("#parent-container").on("click", "a",
delegatedClickHandlerForAjax);
 Use Promise interface
$.ajax({ ... }).then(successHandler, failureHandler);
// OR
var jqxhr = $.ajax({ ... });
jqxhr.done(successHandler);
jqxhr.fail(failureHandler);
AJAX
 Sample Ajax Template:
var jqxhr = $.ajax({
url: url,
type: "GET", // default is GET but you can use other verbs based on your needs.
cache: true, // default is true, but false for dataType 'script' and 'jsonp', so set it on need
basis.
data: { }, // add your request parameters in the data object.
dataType: "json", // specify the dataType for future reference
jsonp: "callback", // only specify this to match the name of callback parameter your API is
expecting for JSONP requests. //
statusCode: { // if you want to handle specific error codes, use the status code mapping
settings.
404: handler404,
500: handler500
} });
jqxhr.done(successHandler);
EFFECTS AND ANIMATIONS
 Adopt a restrained and consistent approach to implementing
animation functionality.
 DO NOT over-do the animation effects until driven by the UX
requirements.
1 Try to use simple show/hide, toggle and
slideUp/slideDown
functionality to toggle elements.
2 Try to use predefined animations durations of "slow",
"fast" or 400
(for medium).
CHAINING
 Use chaining as an alternative to variable caching and multiple
selector calls.
$("#myDiv").addClass("error").show();
 Whenever the chain grows over 3 links or gets complicated because
of event assignment, use appropriate line breaks and indentation to
make the code readable.
$("#myLink")
.addClass("bold")
.on("click", myClickHandler)
.on("mouseover", myMouseOverHandler)
.show();
 For long chains it is acceptable to cache intermediate objects in a
variable.
PLUGINS
 Always choose a plugin with good support, documentation,
testing and community support.
 Check the compatibility of plugin with the version of jQuery
that you are using.
 Any common reusable component should be implemented as
a jQuery plugin
MISCELLANEOUS
 Use Object literals for parameters.
$myLink.attr("href", "#").attr("title", "my link").attr("rel", "external"); // BAD, 3 calls to attr()
// GOOD, only 1 call to attr()
$myLink.attr({
href: "#",
title: "my link",
rel: "external"
});
 Do not mix CSS with jQuery.
$("#mydiv").css({'color':red, 'font-weight':'bold'}); // BAD
$("#mydiv").addClass("error"); // GOOD
 DO NOT use Deprecated Methods. It is always important to keep an eye on deprecated
methods for each new version and try avoid using them.Click here for a list of deprecated
methods.
 Combine jQuery with native JavaScript when needed. See the performance difference for
the example given below
$("#myId"); // is still little slower than... document.getElementById("myId");

More Related Content

What's hot

jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009Remy Sharp
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performanceYehuda Katz
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery FundamentalsGil Fink
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do MoreRemy Sharp
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajaxbaygross
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuerySudar Muthu
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgetsvelveeta_512
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuerymanugoel2003
 
Jquery plugin development
Jquery plugin developmentJquery plugin development
Jquery plugin developmentMd. Ziaul Haq
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQueryRemy Sharp
 
JQuery plugin development fundamentals
JQuery plugin development fundamentalsJQuery plugin development fundamentals
JQuery plugin development fundamentalsBastian Feder
 

What's hot (20)

jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
Drupal, meet Assetic
Drupal, meet AsseticDrupal, meet Assetic
Drupal, meet Assetic
 
jQuery
jQueryjQuery
jQuery
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
22 j query1
22 j query122 j query1
22 j query1
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Jquery plugin development
Jquery plugin developmentJquery plugin development
Jquery plugin development
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
JQuery plugin development fundamentals
JQuery plugin development fundamentalsJQuery plugin development fundamentals
JQuery plugin development fundamentals
 

Similar to JQUERY BEST PRACTICES AND SELECTORS GUIDE

[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Writing JavaScript that doesn't suck
Writing JavaScript that doesn't suckWriting JavaScript that doesn't suck
Writing JavaScript that doesn't suckRoss Bruniges
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScriptAndrew Dupont
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Thinqloud
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery ApplicationsRebecca Murphey
 
Reliable Javascript
Reliable Javascript Reliable Javascript
Reliable Javascript Glenn Stovall
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuerykolkatageeks
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQueryKnoldus Inc.
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Domkaven yan
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentNuvole
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for JoomlaLuke Summerfield
 

Similar to JQUERY BEST PRACTICES AND SELECTORS GUIDE (20)

Digesting jQuery
Digesting jQueryDigesting jQuery
Digesting jQuery
 
Frontin like-a-backer
Frontin like-a-backerFrontin like-a-backer
Frontin like-a-backer
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
DrupalCon jQuery
DrupalCon jQueryDrupalCon jQuery
DrupalCon jQuery
 
Writing JavaScript that doesn't suck
Writing JavaScript that doesn't suckWriting JavaScript that doesn't suck
Writing JavaScript that doesn't suck
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery Applications
 
Reliable Javascript
Reliable Javascript Reliable Javascript
Reliable Javascript
 
jQuery
jQueryjQuery
jQuery
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
 
Jquery Basics
Jquery BasicsJquery Basics
Jquery Basics
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven Development
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

JQUERY BEST PRACTICES AND SELECTORS GUIDE

  • 1. JQUERY BEST PRACTICE AND SELECTORS Author: Chandra Shekher P © chandrashekher
  • 2. TOPICS 1. Loading jQuery 2. Variables 3. Selectors 4. Dom Manipulation 5. Events 6. Ajax 7. Animations 8. Plugins 9. Chaining 10. Miscellaneous
  • 3. LOADING JQUERY  Always try to use a CDN to include jQuery on your page, <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <script>window.jQuery || document.write('<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>')</script>  If possible, keep all your JavaScript and jQuery includes at the bottom of your page.  For advanced browser feature detection, use Modernizr.js .  If you are using other libraries like Prototype, MooTools, Zepto etc. that uses $ sign as well,  try not to use $ for calling jQuery functions and instead use jQuery simply. You can return control of $ back to the other library with a call to $.noConflict().
  • 4. JQUERY VARIABLES  All variables that are used to store/cache jQuery objects should have a name prefixed with a $.  Always cache your jQuery selector returned objects in variables for reuse. var $myDiv = $("#myDiv"); $myDiv.click(function(){...});  Use camel case for naming variables.
  • 5. SELECTORS  Use ID selector whenever possible. It is faster because they are handled using document.getElementById().  When using class selectors, don't use the element type in your selector. for Performance Improvements. var $products = $("div.products"); // SLOW var $products = $(".products"); // FAST  Use find for Id->Child nested selectors. The .find() approach is faster because the first selection is handled without going through the Sizzle selector engine. // BAD, a nested query for Sizzle selector engine var $productIds = $("#products div.id"); // GOOD, #products is already selected by document.getElementById() so only div.id needs to go through Sizzle selector engine var $productIds = $("#products").find("div.id");
  • 6. SELECTORS  Be specific on the right-hand side of your selector, and less specific on the left // Unoptimized $("div.data .gonzalez"); // Optimized $(".data td.gonzalez");  Avoid Excessive Specificity $(".data table.attendees td.gonzalez"); // Better: Drop the middle if possible. $(".data td.gonzalez");
  • 7. SELECTORS  Give your Selectors a Context. // SLOWER because it has to traverse the whole DOM for .class $('.class'); // FASTER because now it only looks under class-container. $('.class', '#class-container');  Avoid Universal Selectors $('div.container > *'); // BAD $('div.container').children(); // BETTER
  • 8. SELECTORS  Avoid Implied Universal Selectors. When you leave off the selector, the universal selector (*) is still implied. $('div.someclass :radio'); // BAD $('div.someclass input:radio'); // GOOD Don’t Descend Multiple IDs or nest when selecting an ID. ID-only selections are handled using document.getElementById() so don't mix them with other selectors. $('#outer #inner'); // BAD $('div#inner'); // BAD $('.outer-container #inner'); // BAD $('#inner'); // GOOD, only calls document.getElementById()
  • 9. DOM MANIPULATION  Always detach any existing element before manipulation and attach it back after manipulating it. var $myList = $("#list-container > ul").detach(); //...a lot of complicated things on $myList $myList.appendTo("#list-container");  Don’t Act on Absent Elements. // BAD: This runs three functions before it realizes there's nothing in the selection $("#nosuchthing").slideUp(); // GOOD var $mySelection = $("#nosuchthing"); if ($mySelection.length) { $mySelection.slideUp(); }
  • 10. DOM MANIPULATION  Use string concatenation or array.join() over .append() // BAD var $myList = $("#list"); for(var i = 0; i < 10000; i++){ $myList.append("<li>"+i+"</li>"); } // GOOD var $myList = $("#list"); var list = ""; for(var i = 0; i < 10000; i++){ list += "<li>"+i+"</li>"; } $myList.html(list); // EVEN FASTER var array = []; for(var i = 0; i < 10000; i++){ array[i] = "<li>"+i+"</li>"; } $myList.html(array.join(''));
  • 11. EVENTS  Use only one Document Ready handler per page. It makes it easier to debug and keep track of the behavior flow.  DO NOT use anonymous functions to attach events. Anonymous functions are difficult to debug, maintain, test, or reuse. $("#myLink").on("click", function(){...}); // BAD // GOOD function myLinkClickHandler(){...} $("#myLink").on("click", myLinkClickHandler);  Document ready event handler should not be an anonymous function. Once again, anonymous functions are difficult to debug, maintain, test, or reuse. $(function(){ ... }); // BAD: You can never reuse or write a test for this function. // GOOD $(initPage); // or $(document).ready(initPage); function initPage(){ // Page load event where you can initialize values and call other initializers.
  • 12. EVENTS  Document ready event handlers should be included from external files and inline JavaScript can be used to call the ready handle after any initial setup. <script src="my-document-ready.js"></script> <script> // Any global variable set-up that might be needed. $(document).ready(initPage); // or $(initPage); </script>  DO NOT use behavioral markup in HTML (JavaScript inlining), these are debugging nightmares. Always bind events with jQuery to be consistent so it's easier to attach and remove events dynamically. <a id="myLink" href="#" onclick="myEventHandler();"> my link </a> <!-- BAD --> $("#myLink").on("click", myEventHandler); // GOOD
  • 13. EVENTS  When possible, use custom namespace for events. It's easier to unbind the exact event that you attached without affecting other events bound to the DOM element. $("#myLink").on("click.mySpecialClick", myEventHandler); // GOOD // Later on, it's easier to unbind just your click event $("#myLink").unbind("click.mySpecialClick");  Use event delegation when you have to attach same event to multiple elements. Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future. $("#list a").on("click", myClickHandler); // BAD, you are attaching an event to all the links under the list. $("#list").on("click", "a", myClickHandler); // GOOD, only one event handler is attached to the parent.
  • 14. AJAX  Avoid using .getJson() or .get(), simply use the $.ajax() as that's what gets called internally.  DO NOT use http requests on https sites. Prefer schemaless URLs (leave the protocol http/https out of your URL)  DO NOT put request parameters in the URL, send them using data object setting. // Less readable... $.ajax({ url: "something.php?param1=test1&param2=test2", .... }); // More readable... $.ajax({ url: "something.php", data: { param1: test1, param2: test2 } });
  • 15. AJAX  Try to specify the dataType setting so it's easier to know what kind of data you are working with.  Use Delegated event handlers for attaching events to content loaded using Ajax. Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time $("#parent-container").on("click", "a", delegatedClickHandlerForAjax);  Use Promise interface $.ajax({ ... }).then(successHandler, failureHandler); // OR var jqxhr = $.ajax({ ... }); jqxhr.done(successHandler); jqxhr.fail(failureHandler);
  • 16. AJAX  Sample Ajax Template: var jqxhr = $.ajax({ url: url, type: "GET", // default is GET but you can use other verbs based on your needs. cache: true, // default is true, but false for dataType 'script' and 'jsonp', so set it on need basis. data: { }, // add your request parameters in the data object. dataType: "json", // specify the dataType for future reference jsonp: "callback", // only specify this to match the name of callback parameter your API is expecting for JSONP requests. // statusCode: { // if you want to handle specific error codes, use the status code mapping settings. 404: handler404, 500: handler500 } }); jqxhr.done(successHandler);
  • 17. EFFECTS AND ANIMATIONS  Adopt a restrained and consistent approach to implementing animation functionality.  DO NOT over-do the animation effects until driven by the UX requirements. 1 Try to use simple show/hide, toggle and slideUp/slideDown functionality to toggle elements. 2 Try to use predefined animations durations of "slow", "fast" or 400 (for medium).
  • 18. CHAINING  Use chaining as an alternative to variable caching and multiple selector calls. $("#myDiv").addClass("error").show();  Whenever the chain grows over 3 links or gets complicated because of event assignment, use appropriate line breaks and indentation to make the code readable. $("#myLink") .addClass("bold") .on("click", myClickHandler) .on("mouseover", myMouseOverHandler) .show();  For long chains it is acceptable to cache intermediate objects in a variable.
  • 19. PLUGINS  Always choose a plugin with good support, documentation, testing and community support.  Check the compatibility of plugin with the version of jQuery that you are using.  Any common reusable component should be implemented as a jQuery plugin
  • 20. MISCELLANEOUS  Use Object literals for parameters. $myLink.attr("href", "#").attr("title", "my link").attr("rel", "external"); // BAD, 3 calls to attr() // GOOD, only 1 call to attr() $myLink.attr({ href: "#", title: "my link", rel: "external" });  Do not mix CSS with jQuery. $("#mydiv").css({'color':red, 'font-weight':'bold'}); // BAD $("#mydiv").addClass("error"); // GOOD  DO NOT use Deprecated Methods. It is always important to keep an eye on deprecated methods for each new version and try avoid using them.Click here for a list of deprecated methods.  Combine jQuery with native JavaScript when needed. See the performance difference for the example given below $("#myId"); // is still little slower than... document.getElementById("myId");