SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie Bcblackpool jquery 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
 

Ähnlich wie Bcblackpool jquery 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
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
&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
 
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
 

Kürzlich hochgeladen

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Bcblackpool jquery 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)

Hinweis der Redaktion

  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.