SlideShare una empresa de Scribd logo
1 de 57
Pimp my Ride jQuery
     By @Jack_Franklin
Load from Google
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
• Chances are user may already have it
  cached.
Use of CSS SelectorS!
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
• Be aware of the efficiency.
Save Selectors
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
• Nicer:
  var elem = $(“myelem”);
  elem.hide();
  elem.css(“width”, “200”);
  elem.show();
Or Chain
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
• Either use this or caching to variables -
  whichever suits you and/or the situation
  best.
Chain over Multiple
       Lines
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
• $(“elem”)
     .hide()
     .addClass()
  //etc
Context
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
• This looks for “myelem” within
  someCachedElement.
Context Continued
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
• EG:
  $(“myelem”, “#somediv”);
  jQuery will only search inside #somediv for
  “myelem”.
Context Continued
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
• So the best way is to skip that step and just
  do:
  $(context).find(selector);
Clever Traversing




  http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.




          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();


          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();
• Can easily be made much nicer:
          http://api.jquery.com/category/traversing/
Clever Traversing
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
• For example - the functions find() and
  children().
Avoid Class Selection




 http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).




  http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).
• As jQuery can rely on getElementById &
  getElementsByTagName select by ID or Tag
  if you possibly can.

   http://www.quirksmode.org/dom/w3c_core.html
Check if an Element
      Exists
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
• jQuery gracefully degrades - if it does not
  exist no errors thrown and nothing breaks.
Run onLoad
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
• Shorten this to:
  $(function() {
  //code here
  }
Apply a Class to
   <body>
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
• On a similar note, if you can make CSS do
  the heavy work, do so. Javascript should do
  the bare minimum.
Store info with
jQuery’s data method
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
• And it can be retrieved like so:
  $(“myelem”).data(“usefulstuff”);
Surrender the $
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
• (function($) {
  //use $ as normal here for jQuery
  })(jQuery);
  //out here $ is not relating to jQuery
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
      http://www.scriptalizer.com/
Links & Resources

• Official site: www.jquery.com
• jQuery Docs: docs.jquery.com
• www.learningjquery.com
• net.tutsplus.com (hit and miss)
Thanks!
  @Jack_Franklin
www.jackfranklin.co.uk

Más contenido relacionado

La actualidad más candente

Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do MoreRemy Sharp
 
An in-depth look at jQuery UI
An in-depth look at jQuery UIAn in-depth look at jQuery UI
An in-depth look at jQuery UIPaul Bakaus
 
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 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
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery FundamentalsGil Fink
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
 
The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryQConLondon2008
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
jQuery (MeshU)
jQuery (MeshU)jQuery (MeshU)
jQuery (MeshU)jeresig
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterpriseDave Artz
 

La actualidad más candente (20)

jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
An in-depth look at jQuery UI
An in-depth look at jQuery UIAn in-depth look at jQuery UI
An in-depth look at jQuery UI
 
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
jQueryjQuery
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 Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J Query
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
jQuery (MeshU)
jQuery (MeshU)jQuery (MeshU)
jQuery (MeshU)
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
 
jQuery
jQueryjQuery
jQuery
 

Destacado

jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010Jack Franklin
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! Jack Franklin
 
Java script
Java scriptJava script
Java scriptKumar
 
Designing for the Teenage Market
Designing for the Teenage MarketDesigning for the Teenage Market
Designing for the Teenage MarketJack Franklin
 
Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8Jack Franklin
 
How do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital worldHow do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital worldPäivi Aarreniemi-Jokipelto
 

Destacado (6)

jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better!
 
Java script
Java scriptJava script
Java script
 
Designing for the Teenage Market
Designing for the Teenage MarketDesigning for the Teenage Market
Designing for the Teenage Market
 
Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8
 
How do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital worldHow do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital world
 

Similar a Pimp My Ride with jQuery Optimization Tips

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
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationMevin Mohan
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for DrupalSergey Semashko
 
Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3luckysb16
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone InteractivityEric Steele
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developersDream Production AG
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patternsStoyan Stefanov
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptxtutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptxLe Hung
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 

Similar a Pimp My Ride with jQuery Optimization Tips (20)

J query
J queryJ query
J query
 
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...
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
Jquery
JqueryJquery
Jquery
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developers
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patterns
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 

Último

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 slidevu2urc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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 Scriptwesley chun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
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.pptxEarley Information Science
 

Último (20)

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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 

Pimp My Ride with jQuery Optimization Tips

  • 1. Pimp my Ride jQuery By @Jack_Franklin
  • 3. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js">
  • 4. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish.
  • 5. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish. • Chances are user may already have it cached.
  • 6. Use of CSS SelectorS!
  • 7. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector
  • 8. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector.
  • 9. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”);
  • 10. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”); • Be aware of the efficiency.
  • 12. Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show();
  • 13. Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show(); • Nicer: var elem = $(“myelem”); elem.hide(); elem.css(“width”, “200”); elem.show();
  • 16. Or Chain • $(“myelem”).hide().css(“width”, “200”).show(); • Either use this or caching to variables - whichever suits you and/or the situation best.
  • 18. Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr();
  • 19. Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr(); • $(“elem”) .hide() .addClass() //etc
  • 21. Context • If you’ve cached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement);
  • 22. Context • If you’ve cached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement); • This looks for “myelem” within someCachedElement.
  • 24. Context Continued • Contextualised selectors should be in the form: $(selector, context)
  • 25. Context Continued • Contextualised selectors should be in the form: $(selector, context) • EG: $(“myelem”, “#somediv”); jQuery will only search inside #somediv for “myelem”.
  • 27. Context Continued • When you do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector).
  • 28. Context Continued • When you do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector). • So the best way is to skip that step and just do: $(context).find(selector);
  • 29. Clever Traversing http://api.jquery.com/category/traversing/
  • 30. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. http://api.jquery.com/category/traversing/
  • 31. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); http://api.jquery.com/category/traversing/
  • 32. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); • Can easily be made much nicer: http://api.jquery.com/category/traversing/
  • 34. Clever Traversing • Be sensible - be sure to use the right function for the right job.
  • 35. Clever Traversing • Be sensible - be sure to use the right function for the right job. • For example - the functions find() and children().
  • 36. Avoid Class Selection http://www.quirksmode.org/dom/w3c_core.html
  • 37. Avoid Class Selection • getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). http://www.quirksmode.org/dom/w3c_core.html
  • 38. Avoid Class Selection • getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). • As jQuery can rely on getElementById & getElementsByTagName select by ID or Tag if you possibly can. http://www.quirksmode.org/dom/w3c_core.html
  • 39. Check if an Element Exists
  • 40. Check if an Element Exists • Easy: if($(“elem”).length) { //element must exist }
  • 41. Check if an Element Exists • Easy: if($(“elem”).length) { //element must exist } • jQuery gracefully degrades - if it does not exist no errors thrown and nothing breaks.
  • 43. Run onLoad • Run your Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now }
  • 44. Run onLoad • Run your Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now } • Shorten this to: $(function() { //code here }
  • 45. Apply a Class to <body>
  • 46. Apply a Class to <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”);
  • 47. Apply a Class to <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”); • On a similar note, if you can make CSS do the heavy work, do so. Javascript should do the bare minimum.
  • 49. Store info with jQuery’s data method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”)
  • 50. Store info with jQuery’s data method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”) • And it can be retrieved like so: $(“myelem”).data(“usefulstuff”);
  • 52. Surrender the $ • jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide();
  • 53. Surrender the $ • jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide(); • (function($) { //use $ as normal here for jQuery })(jQuery); //out here $ is not relating to jQuery
  • 54. Combine & Minify • Once you’re done, combining your scripts into one and then minifying them is a big help.
  • 55. Combine & Minify • Once you’re done, combining your scripts into one and then minifying them is a big help. http://www.scriptalizer.com/
  • 56. Links & Resources • Official site: www.jquery.com • jQuery Docs: docs.jquery.com • www.learningjquery.com • net.tutsplus.com (hit and miss)

Notas del editor

  1. Point out that it&amp;#x2019;s mainly for the beginners/intermediate users. Feel free to throw in your own tips at the end.
  2. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  3. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  4. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  5. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  6. Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  7. Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  8. If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  9. If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  10. Completely optional - your call
  11. Completely optional - your call
  12. Just continuing from caching
  13. Just continuing from caching
  14. End with however...
  15. End with however...
  16. You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  17. You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  18. Browse the traversing docs - URL there - so many functions.
  19. Browse the traversing docs - URL there - so many functions.
  20. Browse the traversing docs - URL there - so many functions.
  21. find() will descend right through the DOM children() only descends one level
  22. find() will descend right through the DOM children() only descends one level
  23. getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  24. getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  25. Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  26. Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  27. Easy quick way to detect if javascript is on. (Most people do have it on)
  28. Easy quick way to detect if javascript is on. (Most people do have it on)
  29. Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  30. Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  31. For use with other libraries
  32. For use with other libraries
  33. A quick google search for tutorials finds you loads.