SlideShare ist ein Scribd-Unternehmen logo
1 von 15
10 Time-Savers You (Maybe) Don't Know




           Girish Gangadharan


@appoosa        girish@giri.sh      http://giri.sh
Limit DOM traversal

var $items = $(„#products‟);

$items.click(function() {
          $(this).next(„div‟).slideToggle();
});

$items.addClass(„active‟);

$items.closest(„p#category‟).animate({ height:100px });
Less code. Better readability.

var $userprofile = $(„#user-profile‟);
$userprofile
     .hover(function () {
                 $(this).addClass(“highlight");
            },
            function () {
                 $(this).removeClass(“highlight");
      })
     .click(function() {
          //do something
     })
     .fadeIn(„slow‟);
Don‟t leave them hanging.


    :first-child, :last-child, :even, :odd, :parent, :next, :prev, etc.



Precede them with a tag name or some selector;
      else the universal selector is implied.

                     $(':focus') = $('*:focus')
Understand what each does. Use appropriately.

         $('#sometable').each(function(){
                $('td', this).bind('hover', function(){
                      $(this).toggleClass('hover');
                });
          });
         $('#sometable').each(function(){
                $('td', this).live('hover', function(){
                      $(this).toggleClass('hover');
                });
          });
         $('#sometable').delegate('td', 'hover', function(){
                $(this).toggleClass('hover');
         });
Create in memory. Then update the DOM.

var $mylist = $('#mylist'); // <ul>
for (var i = 0; i < 100; i++) {
           $mylist.append('<li>' + bestsellers[i] + '</li>');
}



var $mylist = $('#mylist'); // <ul>
var concatenatedList = “”;
for (var i = 0; i < 100; i++) {
          concatenatedList += ('<li>' + bestsellers[i] + '</li>');
}
$mylist.append(concatenatedList);
Bind less.


$(„#reports td‟).bind(„click‟, function() {
      //do something
})




$(„#reports‟).bind(„click‟, function(e) {
     var target = e.target;
     if (target.nodeName === „td') {
                // do something
     }
})
Choose your event carefully.


$(document).ready(function() {
     //Fires as soon as the DOM is ready
     //Doesn‟t wait for images, style sheets etc.
})


$(window).load(function() {
     //Fires after all the content is loaded
     //Includes images, style sheets, etc.
})
Think right-to-left (except for IDs)



 $('#user-profiles > li > input:disabled');




$('#user-profiles‟).find(„li > input:disabled');
Sizzle engine is highly optimized but


var $someElement = $(„#elementId‟);
                 Vs.
var someElement = document.getElementById(„elementId‟);



                 $(„#elementId‟).css(„display‟, „block‟);
                                      Vs.
                 document.getElementById(„elementId‟).style.display = „block‟;


             Do you really need a whole library to
                   accomplish your tasks?
Will this improve page performance by x times?

                        Probably, not.
(especially if you don‟t have a complex multi-nested DOM)




       For example, how to better structure your code.
           ‱ Module Pattern
           ‱ Revealing Module Pattern
           ‱ Constructional Pattern
           ‱ Creational Pattern
           ‱ Factory Pattern
           ‱ etc.
Essential JavaScript Design Patterns: (Free!)
http://addyosmani.com/blog/essentialjsdesignpatternsupdate1


jQuery: Novice to Ninja
http://www.amazon.com/jQuery-Novice-Ninja-Earle-Castledine/dp/0980576857



                            jQuery API
                            http://api.jquery.com



                            JavaScript Performance Testing
                            http://jsperf.com/browse
10 Time-Savers You (Maybe) Don't Know




           Girish Gangadharan


@appoosa        girish@giri.sh      http://giri.sh

Weitere Àhnliche Inhalte

Was ist angesagt?

How to learn j query
How to learn j queryHow to learn j query
How to learn j query
Baoyu Xu
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Siva Arunachalam
 
Intro to jquery
Intro to jqueryIntro to jquery
Intro to jquery
Dan Pickett
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
taggg
 

Was ist angesagt? (20)

jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
How to learn j query
How to learn j queryHow to learn j query
How to learn j query
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
jQuery Foot-Gun Features
jQuery Foot-Gun FeaturesjQuery Foot-Gun Features
jQuery Foot-Gun Features
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
 
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
 
Jquery
JqueryJquery
Jquery
 
Intro to jquery
Intro to jqueryIntro to jquery
Intro to jquery
 
J queryui
J queryuiJ queryui
J queryui
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
 
Jquery introduction
Jquery introductionJquery introduction
Jquery introduction
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
 
Jquery-overview
Jquery-overviewJquery-overview
Jquery-overview
 

Andere mochten auch

PresentaciĂłn Gonzalo Cubillos
PresentaciĂłn Gonzalo CubillosPresentaciĂłn Gonzalo Cubillos
PresentaciĂłn Gonzalo Cubillos
Visnja Tomicic
 
Building your-personal-brand-ver-0-7
Building your-personal-brand-ver-0-7Building your-personal-brand-ver-0-7
Building your-personal-brand-ver-0-7
Aryandra Anantama
 
Taking Away The Keys
Taking Away The KeysTaking Away The Keys
Taking Away The Keys
Laurenwatral
 

Andere mochten auch (13)

PresentaciĂłn Gonzalo Cubillos
PresentaciĂłn Gonzalo CubillosPresentaciĂłn Gonzalo Cubillos
PresentaciĂłn Gonzalo Cubillos
 
Ghanshyam Asked To Krishna
Ghanshyam Asked To KrishnaGhanshyam Asked To Krishna
Ghanshyam Asked To Krishna
 
Building your-personal-brand-ver-0-7
Building your-personal-brand-ver-0-7Building your-personal-brand-ver-0-7
Building your-personal-brand-ver-0-7
 
#Ti2016 Tijuana Innovadora 2016, Creativa
#Ti2016 Tijuana Innovadora 2016, Creativa#Ti2016 Tijuana Innovadora 2016, Creativa
#Ti2016 Tijuana Innovadora 2016, Creativa
 
Le collectif du 4 Décembre écrit au pouvoir législatif
Le collectif du 4 Décembre écrit au pouvoir législatifLe collectif du 4 Décembre écrit au pouvoir législatif
Le collectif du 4 Décembre écrit au pouvoir législatif
 
Socializacion del proyecto de educatic martha reyes
Socializacion del proyecto de educatic martha reyesSocializacion del proyecto de educatic martha reyes
Socializacion del proyecto de educatic martha reyes
 
Taking Away The Keys
Taking Away The KeysTaking Away The Keys
Taking Away The Keys
 
Learning Analytics at KULeuven by the team of Erik Duval
Learning Analytics at KULeuven by the team of Erik DuvalLearning Analytics at KULeuven by the team of Erik Duval
Learning Analytics at KULeuven by the team of Erik Duval
 
C.Harris
C.HarrisC.Harris
C.Harris
 
Présentation Schéma Directeur Open Source au S2LQ 2015
Présentation Schéma Directeur Open Source au S2LQ 2015Présentation Schéma Directeur Open Source au S2LQ 2015
Présentation Schéma Directeur Open Source au S2LQ 2015
 
El nazismo breve resumen
El nazismo breve resumenEl nazismo breve resumen
El nazismo breve resumen
 
Safety with a Star (DE)
Safety with a Star (DE)Safety with a Star (DE)
Safety with a Star (DE)
 
Mayra ,,,,,,,,
Mayra ,,,,,,,,Mayra ,,,,,,,,
Mayra ,,,,,,,,
 

Ähnlich wie jQuery - 10 Time-Savers You (Maybe) Don't Know

Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
Chris Coyier
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
Jarod Ferguson
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
Justin Cataldo
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
Remy Sharp
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
brinsknaps
 

Ähnlich wie jQuery - 10 Time-Savers You (Maybe) Don't Know (20)

Jquery optimization-tips
Jquery optimization-tipsJquery optimization-tips
Jquery optimization-tips
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuery
 
jQuery
jQueryjQuery
jQuery
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
 
J query1
J query1J query1
J query1
 
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...
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
J query training
J query trainingJ query training
J query training
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
 
Javascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularJavascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & Angular
 

Mehr von girish82 (8)

Introduction to node
Introduction to nodeIntroduction to node
Introduction to node
 
Designing better-ux
Designing better-uxDesigning better-ux
Designing better-ux
 
Designing better-ux-workshop-2
Designing better-ux-workshop-2Designing better-ux-workshop-2
Designing better-ux-workshop-2
 
Designing better-ux-workshop-3
Designing better-ux-workshop-3Designing better-ux-workshop-3
Designing better-ux-workshop-3
 
Designing better-ux-workshop-4
Designing better-ux-workshop-4Designing better-ux-workshop-4
Designing better-ux-workshop-4
 
Designing better-ux-workshop-5
Designing better-ux-workshop-5Designing better-ux-workshop-5
Designing better-ux-workshop-5
 
Why we need to hire UX professionals
Why we need to hire UX professionalsWhy we need to hire UX professionals
Why we need to hire UX professionals
 
jQuery - Doing it right
jQuery - Doing it rightjQuery - Doing it right
jQuery - Doing it right
 

KĂŒrzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

KĂŒrzlich hochgeladen (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
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?
 
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
 
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
 

jQuery - 10 Time-Savers You (Maybe) Don't Know

  • 1. 10 Time-Savers You (Maybe) Don't Know Girish Gangadharan @appoosa girish@giri.sh http://giri.sh
  • 2. Limit DOM traversal var $items = $(„#products‟); $items.click(function() { $(this).next(„div‟).slideToggle(); }); $items.addClass(„active‟); $items.closest(„p#category‟).animate({ height:100px });
  • 3. Less code. Better readability. var $userprofile = $(„#user-profile‟); $userprofile .hover(function () { $(this).addClass(“highlight"); }, function () { $(this).removeClass(“highlight"); }) .click(function() { //do something }) .fadeIn(„slow‟);
  • 4. Don‟t leave them hanging. :first-child, :last-child, :even, :odd, :parent, :next, :prev, etc. Precede them with a tag name or some selector; else the universal selector is implied. $(':focus') = $('*:focus')
  • 5. Understand what each does. Use appropriately. $('#sometable').each(function(){ $('td', this).bind('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').each(function(){ $('td', this).live('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').delegate('td', 'hover', function(){ $(this).toggleClass('hover'); });
  • 6. Create in memory. Then update the DOM. var $mylist = $('#mylist'); // <ul> for (var i = 0; i < 100; i++) { $mylist.append('<li>' + bestsellers[i] + '</li>'); } var $mylist = $('#mylist'); // <ul> var concatenatedList = “”; for (var i = 0; i < 100; i++) { concatenatedList += ('<li>' + bestsellers[i] + '</li>'); } $mylist.append(concatenatedList);
  • 7. Bind less. $(„#reports td‟).bind(„click‟, function() { //do something }) $(„#reports‟).bind(„click‟, function(e) { var target = e.target; if (target.nodeName === „td') { // do something } })
  • 8. Choose your event carefully. $(document).ready(function() { //Fires as soon as the DOM is ready //Doesn‟t wait for images, style sheets etc. }) $(window).load(function() { //Fires after all the content is loaded //Includes images, style sheets, etc. })
  • 9. Think right-to-left (except for IDs) $('#user-profiles > li > input:disabled'); $('#user-profiles‟).find(„li > input:disabled');
  • 10. Sizzle engine is highly optimized but
 var $someElement = $(„#elementId‟); Vs. var someElement = document.getElementById(„elementId‟); $(„#elementId‟).css(„display‟, „block‟); Vs. document.getElementById(„elementId‟).style.display = „block‟; Do you really need a whole library to accomplish your tasks?
  • 11. Will this improve page performance by x times? Probably, not. (especially if you don‟t have a complex multi-nested DOM) For example, how to better structure your code. ‱ Module Pattern ‱ Revealing Module Pattern ‱ Constructional Pattern ‱ Creational Pattern ‱ Factory Pattern ‱ etc.
  • 12.
  • 13.
  • 14. Essential JavaScript Design Patterns: (Free!) http://addyosmani.com/blog/essentialjsdesignpatternsupdate1 jQuery: Novice to Ninja http://www.amazon.com/jQuery-Novice-Ninja-Earle-Castledine/dp/0980576857 jQuery API http://api.jquery.com JavaScript Performance Testing http://jsperf.com/browse
  • 15. 10 Time-Savers You (Maybe) Don't Know Girish Gangadharan @appoosa girish@giri.sh http://giri.sh