SlideShare ist ein Scribd-Unternehmen logo
1 von 60
jQuery Has
Coding
Standards
Now You Tell Me...
Who am I?
RJ Bruneel
University of Central Florida
Web Applications Developer
MARKETING DEPARTMENT
ALUMNI ’97
RJ.Bruneel@ucf.edu
Why use best
practices &
standards
anyway?
Topics
Loading jQuery
jQuery Variables
jQuery Selectors
jQuery DOM Manipulation
jQuery Events
jQuery AJAX
Miscellaneous
Loading
jQuery
<script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
Loading jQuery from a local server
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
Loading jQuery from a content delivery
network (CDN)
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
Relying solely on a CDN
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
<script>window.jQuery || document.write('<script src="js/jquery-
2.1.4.min.js" type="text/javascript"></script>');</script>
Implementing a fallback
Loading jQuery
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
Using protocol dependent URLs
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
Using protocol independent URLs
Loading jQuery
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
</head>
Loading jQuery at the top of the page
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
</body>
</html>
Loading jQuery at the bottom of the
page
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
type="text/javascript"></script>
Loading jQuery 2.x if you support
IE 6, 7 or 8
Loading jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"
type="text/javascript"></script>
Loading jQuery 1.x if you support
IE 6, 7 or 8
Loading jQuery
jQuery
Variables
var bigcontainer = $("#container");
Disregarding naming standards
jQuery Variables
var $bigContainer = $("#container");
Following naming standards
jQuery Variables
for(var i = 0; i < 50; i++) {
$("#container").text(i);
}
Searching the DOM for the same
element more than once
jQuery Variables
var $container = $("#container");
for(var i = 0; i < 50; i++) {
$container.text(i);
}
Caching jQuery objects using variables
jQuery Variables
$("#link").addClass("bold");
$("#link").fadeIn();
$("#link").removeClass("bold");
Calling multiple methods on the same
jQuery object
jQuery Variables
$("#link")
.addClass("bold")
.fadeIn()
.removeClass("bold");
Using the chaining feature
jQuery Variables
jQuery
Selectors
$(".products");
Searching the DOM for a single
element with a class name
jQuery Selectors
$("#products");
Searching the DOM using
an ID selector
jQuery Selectors
$("#container table.attendees td.column");
$("#outer-container #inner");
$("div#inner");
Using multiple IDs, excessive specificity
or nesting when selecting an ID
jQuery Selectors
$("#product-container");
$("#product-container").find(".products");
$(".products", "#products-container");
Keeping selectors simple or giving
them a context
jQuery Selectors
$(".container > *");
$(":radio");
$(":checkbox");
Using universal selectors
jQuery Selectors
$(".container").children();
$("input[type=checkbox]");
$("input[type=radio]");
Using the children method and being
more specific
jQuery Selectors
jQuery DOM
Manipulation
var $list = $("#list");
for(var i = 0; i < 1000; i++) {
$list.append("<li>"+i+"</li>");
}
Appending the same element many
times to the DOM
jQuery DOM Manipulation
var array = [];
for(var i = 0; i < 1000; i++){
array[i] = "<li>"+i+"</li>";
}
$list.append(array.join(""));
Using string concatenation or
array.join() before appending
jQuery DOM Manipulation
$("#element-not-found").slideUp();
Running jQuery methods on elements
that don’t exist
jQuery DOM Manipulation
var $noSuchElement = $("#element-not-found");
if ($noSuchElement.length) {
$noSuchElement.slideUp();
}
Verifying objects exist before using
them
jQuery DOM Manipulation
var $list = $("#list-container > ul");
// Complicated DOM manipulation methods
Performing lots of DOM manipulation
inline
jQuery DOM Manipulation
var $list = $("#list-container > ul").detach();
// Complicated DOM manipulation
$list.appendTo("#list-container");
Detaching elements before DOM
manipulation and adding them back
jQuery DOM Manipulation
jQuery
Events
<a id="link" href="#"
onclick="linkEventHandler();">link</a>
Putting javascript in the HTML code
jQuery Events
$("#link").on("click", myEventHandler);
Using jQuery to bind events
jQuery Events
$("#link").on("click", function() {
alert("Link clicked!");
});
Using anonymous functions
jQuery Events
function linkClickHandler() {
alert("Link clicked!");
}
$("#link").on("click", linkClickHandler);
Creating a method for your click
handlers
jQuery Events
$(function() {
alert("DOM is ready");
});
Using anonymous function for the
document ready event handler
jQuery Events
function initPage() {
alert("DOM is ready");
}
$(initPage);
Using named function for the document
ready event handler
jQuery Events
$("#list a").on("click", myClickHandler);
Duplicating event handlers
jQuery Events
$("#list").on("click", "a", myClickHandler);
Using event delegation
jQuery Events
jQuery AJAX
var jqxhr = $.get(url, successHandler);
Using .getJson() or .get()
jQuery AJAX
var jqxhr = $.ajax({
url: url,
success: successHandler
});
jqxhr.done(successHandler);
Using $.ajax()
jQuery AJAX
var jqxhr = $.ajax({
url: "ajaxURL.php?param1=title&param2=name"
});
Putting parameters in the URL
jQuery AJAX
var jqxhr = $.ajax({
url: url,
data: {
param1: "Title",
param1: "Name" }
});
Putting parameters in the data object
jQuery AJAX
var jqxhr = $.ajax({
url: url
});
jqxhr.done(successHandler);
Leaving off error handling
jQuery AJAX
var jqxhr = $.ajax({
statusCode: {
404: handler404,
500: handler500
}
});
jqxhr.fail(failureHandler);
Implementing error handling
jQuery AJAX
Miscellaneous
$("#error-message").css("color":"red", "font-
weight":"bold");
Using the CSS method in jQuery
Miscellaneous
.error {
"color": "red";
"font-weight": "bold";
}
$("#error-message").addClass("error");
Using addClass method
Miscellaneous
$link
.attr("href", "#")
.attr("title", "My Title")
.attr("rel", "external");
Repeating jQuery methods
Miscellaneous
$link.attr({
"href": "#",
"title": "My Title",
"rel": "external"
});
Using object literals for parameters
Miscellaneous
Questions?
THANK YOU
Download Slides
http://www.slideshare.net/RJBruneel/jquery-has-coding-standards
Additional Resources
http://www.jquery.com
http://lab.abhinayrathore.com/jquery-standards/
http://gregfranko.com/jquery-best-practices/
Contact
RJBruneel@ucf.edu

Weitere ähnliche Inhalte

Was ist angesagt?

GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4Heather Rock
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and ModernizrJussi Pohjolainen
 
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
 
WordPress Third Party Authentication
WordPress Third Party AuthenticationWordPress Third Party Authentication
WordPress Third Party AuthenticationAaron Brazell
 
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
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajaxbaygross
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development PracticesBrandon Dove
 
jQuery Mobile with HTML5
jQuery Mobile with HTML5jQuery Mobile with HTML5
jQuery Mobile with HTML5madhurpgarg
 
20111014 mu me_j_querymobile
20111014 mu me_j_querymobile20111014 mu me_j_querymobile
20111014 mu me_j_querymobileErik Duval
 
So long, jQuery, and thanks for all the fish!
So long, jQuery, and thanks for all the fish!So long, jQuery, and thanks for all the fish!
So long, jQuery, and thanks for all the fish!Matt Turnure
 
JavaScript & AJAX in WordPress
JavaScript & AJAX in WordPressJavaScript & AJAX in WordPress
JavaScript & AJAX in WordPressIgor Benić
 
Mume JQueryMobile Intro
Mume JQueryMobile IntroMume JQueryMobile Intro
Mume JQueryMobile IntroGonzalo Parra
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesBrandon Dove
 
Difference between java script and jquery
Difference between java script and jqueryDifference between java script and jquery
Difference between java script and jqueryUmar Ali
 

Was ist angesagt? (20)

GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4
 
JQuery
JQueryJQuery
JQuery
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
 
Jquery Basics
Jquery BasicsJquery Basics
Jquery Basics
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
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
 
WordPress Third Party Authentication
WordPress Third Party AuthenticationWordPress Third Party Authentication
WordPress Third Party Authentication
 
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
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Client Web
Client WebClient Web
Client Web
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development Practices
 
jQuery Mobile with HTML5
jQuery Mobile with HTML5jQuery Mobile with HTML5
jQuery Mobile with HTML5
 
20111014 mu me_j_querymobile
20111014 mu me_j_querymobile20111014 mu me_j_querymobile
20111014 mu me_j_querymobile
 
So long, jQuery, and thanks for all the fish!
So long, jQuery, and thanks for all the fish!So long, jQuery, and thanks for all the fish!
So long, jQuery, and thanks for all the fish!
 
JavaScript & AJAX in WordPress
JavaScript & AJAX in WordPressJavaScript & AJAX in WordPress
JavaScript & AJAX in WordPress
 
Mume JQueryMobile Intro
Mume JQueryMobile IntroMume JQueryMobile Intro
Mume JQueryMobile Intro
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin Pages
 
Difference between java script and jquery
Difference between java script and jqueryDifference between java script and jquery
Difference between java script and jquery
 
Coisas para o blog
Coisas para o blogCoisas para o blog
Coisas para o blog
 

Andere mochten auch

JavaScript Coding Guidelines
JavaScript Coding GuidelinesJavaScript Coding Guidelines
JavaScript Coding GuidelinesOleksii Prohonnyi
 
Freakin Whitespace, Building a JavaScript Style Guide
Freakin Whitespace, Building a JavaScript Style GuideFreakin Whitespace, Building a JavaScript Style Guide
Freakin Whitespace, Building a JavaScript Style Guidelizlux
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelinesbrijraj_singh
 

Andere mochten auch (7)

Java script best practices v4
Java script best practices v4Java script best practices v4
Java script best practices v4
 
JavaScript Coding Guidelines
JavaScript Coding GuidelinesJavaScript Coding Guidelines
JavaScript Coding Guidelines
 
Js coding standards
Js coding standardsJs coding standards
Js coding standards
 
Freakin Whitespace, Building a JavaScript Style Guide
Freakin Whitespace, Building a JavaScript Style GuideFreakin Whitespace, Building a JavaScript Style Guide
Freakin Whitespace, Building a JavaScript Style Guide
 
Coding standard
Coding standardCoding standard
Coding standard
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelines
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 

Ähnlich wie jQuery Has Coding Standards

jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery FundamentalsGil Fink
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009Remy Sharp
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationSean Burgess
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014Mark Rackley
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do MoreRemy Sharp
 
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 BasicsEPAM Systems
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 

Ähnlich wie jQuery Has Coding Standards (20)

jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
JQuery UI
JQuery UIJQuery UI
JQuery UI
 
jQuery quick tuts
jQuery quick tutsjQuery quick tuts
jQuery quick tuts
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
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
 
J query1
J query1J query1
J query1
 
Jqueryppt (1)
Jqueryppt (1)Jqueryppt (1)
Jqueryppt (1)
 
webstudy jquery
webstudy jquerywebstudy jquery
webstudy jquery
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
jQuery
jQueryjQuery
jQuery
 
jQuery
jQueryjQuery
jQuery
 
Jquery beltranhomewrok
Jquery beltranhomewrokJquery beltranhomewrok
Jquery beltranhomewrok
 
Jquery beltranhomewrok
Jquery beltranhomewrokJquery beltranhomewrok
Jquery beltranhomewrok
 
J query training
J query trainingJ query training
J query training
 

Kürzlich hochgeladen

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 

jQuery Has Coding Standards

Hinweis der Redaktion

  1. According to builtwith.com jQuery’s core library is used by about 70% of the top 100,000 web sites, up 10% from last year. Several projects use jQuery including Bootstrap, Backbone.js, jQuery Mobile and many plugins have been developed to support jQuery. How many of you are currently developing web sites with jQuery? When used correctly, jQuery can make it easier to build interesting and interactive web sites. Today I am going to share some good and bad ideas when developing web sites using the jQuery framework.
  2. If developers follow best practices and standards it makes code easier to read and comprehend. Code bases should appear as if it were written by a single programmer, rather than a group of competing coders. I have consolidated several examples of how to improve maintainability and performance using best practices and standards. I have collected these from several web sites and blog posts I have perused throughout my career as a developer.
  3. I wish I had known about jQuery coding standards and best practices when I first started developing with jQuery. Although back when I first started developing with jQuery they probably didn’t exist. I would get frustrated when I would find three or four different ways to write the same bit of logic and I wish I had some guidelines to follow. With this presentation I hope to help you avoid the frustration I went through when I started developing with jQuery. I will cover best practices and standards related to the following topics...
  4. Loading jQuery from a local server can be slow.
  5. Loading jQuery from a CDN is much faster because CDNs use very powerful, geographically distributed servers to deliver jQuery to your users. Since many web sites use CDNs the jQuery library will most likely have already been cached by the browser and may not even need to be downloaded at all. In this code snippet you can see that jQuery is loaded from Google’s CDN which hosts many frameworks along with jQuery.
  6. It is a bad idea to assume the CDN is going to be up 100% of the time. In the unlikely event that jQuery fails to load from the CDN your web site is probably going to look pretty bad.
  7. This code example demonstrates how to verify if jQuery has been loaded. And if jQuery fails to load a script tag is written out to load jQuery from your local server Explain the code.
  8. Protocol dependent URLs are URLs that contain the http: or https: portion of the URL. If your web page is loaded using the https: protocol and jQuery is loaded using http: protocol. Your users will get a nasty error message similar to the one displayed here.
  9. Leave off the http: or https: portion off the URL The browser will use the same protocol that was used to load the html No more ugly error message
  10. This is less of an issue with modern browsers, but unfortunately not all of our users are using modern browsers. If the script is loaded at the top of the page the browser will download it before it downloads other resources such as the HTML and CSS. The user will be starring at a blank page while they are waiting for jQuery to downloaded and run.
  11. When jQuery is loaded at the bottom of the page the browser will download the HTML and CSS before it downloads the JavaScript. The page will render faster allowing the user to view the page more quickly because the HTML and CSS will load and draw the page before the JavaScript code is downloaded.
  12. In order to make jQuery more efficient for sites that don’t support IE8 and below the that code has been removed in version 2.x Your code will NOT run as expected on browsers less than IE9.
  13. The jQuery foundation was nice enough to produce a version of the framework with support for IE 8 and below. Use jQuery 1.x if you intend to support IE 6, 7 and 8.
  14. In this example the big container variable is difficult to read And it is not clear that it is a jQuery object
  15. It is much easier to read the big container variable because it is written using camel case. A common jQuery standard is to preface jQuery object variable names with a $. This makes it easy to identify that this variable is a jQuery object.
  16. Searching the DOM is slow therefore you want to do it less often. In this code example jQuery will search the DOM for an element with an id of container 50 times.
  17. In this example jQuery searches the DOM once for the element The jQuery object is saved to a variable in the first line of code Then inside the for loop the variable is referenced instead of searching the DOM for the element each time.
  18. In this code example jQuery will search the DOM three times for the same object. Once when the addClass method is called. Then again when the fadeIn method is used. And one more time for the removeClass method.
  19. jQuery methods return the object that it references This allows you to chain jQuery methods. In this example jQuery will search the DOM for an element with an ID of link. Then it will run the addClass method, pass the jQuery object to the fadeIn method and finally to the removeClass method. The DOM is searched one time before calling the three methods instead of three times.
  20. If only one element on the page contains the class product this is not the most efficient way to find the element. jQuery will search the entire DOM for the class even after it finds the element.
  21. Searching the DOM by ID is faster because jQuery will call the native JavaScript method document.getElementById(). It is also faster because IDs are unique and jQuery will stop searching the DOM once the element is found. Testing performance using JSPerf.com showed that searching by id is 54% faster than search for a class name.
  22. This is slow since jQuery must search for multiple elements to find what it is looking for. In the first code example jQuery will search for an element with an id of container. And when that is found it will search for a table with a class of attendees and finally a td with a class of column. As you can see there are several elements that need to be found before finding what you are really looking for which is slow.
  23. In the first line of code jQuery will only search for one element and quit searching when it is found. In the second line of code jQuery will search for an element with an ID of product-container and then search for all child elements with a class of products. This is faster than searching the whole DOM for the element with the class of products. Especially if you have several elements in the product container you need to search for. The last line of code is similar to writing the find method and functions similar to it. According to a test on JSPerf.com using the find method is 56% faster than using a child selector.
  24. Searching using universal selectors such as * or pseudo classes such as radio or checkbox is very slow. In the last two lines of code jQuery will search all elements in the DOM to see if they have a type attribute of radio or checkbox.
  25. The first line of code will run faster because the children method has been substituted for the * universal selector. jQuery will first search for the element with the class container and then find all the children in that element. In the last two lines of code the input element has been specified. jQuery will search for input tags on the page first and then check if the input tag has a attribute of type radio or checkbox. Rather than searching all the elements for the attributes. According to a test on JSPerf.com searching for :checkbox is 92% slower than input[type=checkbox] and 68% slower than input:checkbox.
  26. In this example, each time the append method is called, In this case 50 times in the for loop the browser must resize and/or reposition elements. As you can imagine this can be very slow
  27. In this code an array is built containing the list of elements and they are all added to the DOM at the same time. This is faster because the browser only has resize and/or reposition elements once.
  28. In this example jQuery will run three functions before it realizes the element doesn’t exist.
  29. In this example the jQuery object is saved to a variable. On the next line of code we verify the element exists first by checking the length of the selector. This is faster because only one jQuery method is called and the slideUp method is never called.
  30. For example we have a list with many items and run many DOM manipulation methods on it. DOM manipulation is slow. jQuery provide a nice method to make this much more efficient.
  31. Detaching an element from the DOM before manipulating it is faster. Because the browser doesn’t have to refresh the page with each change. The page is refreshed once when the element is added back to the page.
  32. It is difficult to update or remove event handlers, And debug JavaScript if it is embedded in the HTML code.
  33. Updating or removing event handlers is easier if the code is in a JavaScript file. And debugging the JavaScript code is easier if it is all in one place.
  34. If there is one thing you take away from this presentation it is this: Please don’t use anonymous functions. You would think this is a best practice by how many code samples and documentation pages contain anonymous function. They are difficult to debug, maintain, test and are not reusable and Please avoid using them.
  35. As you can see here the click handler references the linkClickHandler function instead of using an anonymous function. Named functions are much easier to debug, maintain and test. As well as the linkClickHandler method in this example can be reused by a hover or keyboard event.
  36. Again anonymous functions are difficult to debug, maintain, test and are not reusable. It is difficult to determine where the document ready event hander ends when your code is contained in a huge anonymous function. Yet I see this all over the Internet.
  37. The document ready event handler can be written with a named function. This will make it easier to break your code up into smaller pieces that are more manageable. I usually start all my jQuery projects with this code.
  38. In this example an event handler is attached to each link within the element with the ID of list. If the list contains 10 items 10 click handlers will be created for this list. This is redundant and can be slow.
  39. In this code snippet a single event handler is added to the list element, And when a user clicks on a link within the list the event will bubble up to the parent list element. This will reduces the number of event handlers on the page, And will be more efficient especially if the list contains many elements. This click handler functions the same except only one click hander is created on the list instead of one for each item in the list.
  40. .getJson and .get are short cuts for $.ajax.
  41. $.ajax is what is actually called, therefore, you might as well use it.
  42. As you can see in this example, It is difficult to read the parameters when they are added as part of the ajax URL.
  43. Parameters are much easier to read in this code snippet because it is added using the data object.
  44. If error handling is not added the ajax will fail silently.
  45. 404, 500 and failure handlers are all implemented here. A friendly message should be added to the error handlers to display to the user, As well as some method to notifying developers of the error. The error could be saved to a log or emailed to the developer.
  46. It is difficult to debug and update your CSS code if it integrated with the JavaScript code.
  47. Create classes in your CSS style sheets, And add them using the jQuery addClass method.
  48. In this example the attr method is called three times. This is inefficient and there is a better way to write this code.
  49. Instead of calling the attr method three times you can pass in an object literal. When you use object literals the attr method is only called once.
  50. Hopefully I have demonstrated how following standards and best practices can make your code more maintainable and efficient. You now should see how your code can be easier to read and comprehend. Now go out there and start writing more efficient code and write it as if it were written by a single programmer, rather than a group of coders.
  51. Code Highlighter: http://tohtml.com/jScript/