SlideShare a Scribd company logo
$ will do everything for us


                  Naga HaRISH
                  ShareOurIdeas.com
Agenda
$ Few words about jQuery
$ Why we need it
$ Optimization tips
  # Load jQuery
  # Using Selectors
  # And more

                               …more worth is waiting…!
$(jQuery)
$ this is a new kind of JavaScript Library.
$ It is a lightweight cross-browser (FF,GC,IE,OP,SF).
$ And it is the most popular JS library in use
  today.
$ It was released in 2006 1st Half and current
  version is 1.7.1

                                                 $.Next()
Each( features )
$ LESS CODE DO MORE
$ We can do Element Styling, Events Handling,
  DOM manipulation, Animations and Ajax.
$ We can develop site rapidly. Because it was
  ready with cool stuff.
$ It is most popular and many awesome plugins.
$ Having good Support and Documentation.
$(“.Tips”) - Load jQuery Script
$ Load minified file, because it small in size (31k
  < 229k)
$ It is best to use Content Delivery
  Network(CDN) URL
$ Let’s Google or Microsoft host jQuery file for U
  # Google src http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
  # Microsoft src http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js
$ Forget About this if you work for local apps
Content Delivery Network
DEMO
jQuery.noConflict()
$ Use no conflict method when you using other
  Libraries.
$ Eg:-
   jQuery.noConflict()
   jQuery(“#Slide6”).next()
   (Or)
   var $j=jQuery.noConflict()
   $j(“#Slide6”).next()
DEMO
Start method
$ Best to use jQuery ready method.
$ Because we can use it more times, where javascript
  page onload is only once.
   $(document).ready(function() {
   //write your code here
   }

   (or)

   $(function(){
   //write your code here
   });
DEMO
Optimize performance, using selectors
$ Optimize selectors to descend from an ID, if
  possible. E.g:- $(“#id ul li”).hide()
$ Use tag names when selecting classes E.g:-
   $(“p.class”).show()

$ We can use comma(,) for multiple selectors
   E.g:- $(“p, ul li,div).css(‘color’,’#000’)


More here http://api.jquery.com/?s=selector
DEMO
Use Chaining
$ Take advantage of Chaining
$ Apply multiple effects and DOM manipulation
  @ a time
   Without Chaining                               With Chaining
   $(document).ready(function () {                $(document).ready(function () {
         $('#id').css('color', #143');                 $('#id').css('color',
         $('#id').html(' Request');               #200').html(' Response')
         $('#id').click(function () {                  .click(function () {
       //something                                    //something
   });                                                 });
   });                                            });
“.css(), .html(…) and more” functions return a jQuery object that can be worked
on instantly. So we use it right away to set the other properties like click event
handler.
DEMO
Use Caching
$ Caching is a great way to limit the number of
  times we need to traverse the DOM to find
  elements matched by the selectors.
  Without Caching                       With Caching
  $(document).ready(function () {       $(document).ready(function () {
        $('#id').css('color', #143');   var id=$('#id');
              :                         id.css('color', #143');
  If(isOk)                                           :
        $('#id').html(' Request');      If(isOk)
              :                               id.html(' Request');
  If(isEnable)                                       :
        $('#id').click(function () {    If(isEnable)
       //something                            id.click(function () {
  }); });                                    //something
                                        }); });
DEMO
Click vs Bind vs Live
$ Click is to handle click event on element
        $('#target').click(function() {
         alert('Handler for .click() called.');
        });
$ Bind is used to like same way, to add events.
  But not only click event. We can create our
  custom events too.
        $('#foo').bind('click', function() { $(this).toggleClass('entered'); });
        or
        $('#foo').bind('valid', function() { //Todo}); and we can fire this
        event like this $(‘#foo’).bind()
Click vs Bind vs Live
$ Live, will work same like Bind.
$ But, Bind can’t add event handler to run time
  add elements.
$ Live will automatically adds handlers for new
  elements.
Event delegation
$ Bind handler only once to the parent element
  containing all the children meant to have
  handlers.
                                           Using Event delegation
  Normal way                               $(document).ready(function () {
  $(document).ready(function () {          $('ul').delegate('click', 'li', function (e) {
  $('ul li').click(function () {               // if ($(e.target).is('li'))
                $(this).toggleClass('hig        $(e.target).toggleClass('highlight');
  hlight');                                     });
  }); });                                  });

$ Advantage of this is it will automatically bind
  click event for new(runtime add) element too.
DEMO
Minimize DOM manipulation
$ We have to minimize DOM operations (.append()
  .prepend() .after() .wrap() and ..)

Without temp variable (full         Without temp variable :-
DOM):-                              var myNumbers = $('#Numbers');
var myNumbers =                     var myString= '';
$('#Numbers');
                            for (i = 0; i < 1000; i++) {
for (i=0; i<1000; i++){       mystring += '<li>Number ' + i + '</li>';
   myNumbers.append('Number }
' + i);
}                           myList.html(myNumbers);
Minimize DOM manipulation(cont..)
$   Use template for array items
$   !dea from Microsoft
$   jQuery add in documentation
$   More http://api.jquery.com/jQuery.template/
$   http://api.jquery.com/jquery.tmpl/
DEMO
Use $(this) and this
$ $(this) is jQuery object
$ this is traditional HTML object

  $(document).ready(function () {
  $('ul li').click(function () {
              $(this).toggleClass('highlight');
  alert(this.innerHTML);
  });
  });
DEMO
Use data
$ Useful. It allows you to bind data to DOM
  elements without modifying the DOM.
$ For example:-
  # $("div").data("message", "Done!");
  # $("div").data("message");
  OR
  # <div data-error=“Error! :(“>…</div>
  # $("div").data("error");
DEMO
$(‘#Thanks’).show();



By Naga HaRISH
ShareOurIdeas.com
More you share, more you gain

More Related Content

What's hot (20)

PPT
jQuery
Mohammed Arif
 
PPTX
Jquery-overview
Isfand yar Khan
 
PPTX
jQuery from the very beginning
Anis Ahmad
 
PDF
D3.js and SVG
Karol Depka Pradzinski
 
PPTX
jQuery
Vishwa Mohan
 
PDF
jQuery Essentials
Marc Grabanski
 
PDF
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
ODP
Drupal Best Practices
manugoel2003
 
PPTX
Jquery introduction
musrath mohammad
 
PDF
History of jQuery
jeresig
 
PDF
jQuery Loves Developers - Oredev 2009
Remy Sharp
 
PDF
Learning jQuery in 30 minutes
Simon Willison
 
KEY
jQuery - Tips And Tricks
Lester Lievens
 
PDF
Introduction to jQuery
Zeeshan Khan
 
PPTX
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
PDF
06 jQuery #burningkeyboards
Denis Ristic
 
PPT
jQuery
Mostafa Bayomi
 
PPTX
jQuery Presentation
Rod Johnson
 
PDF
jQuery Introduction
Arwid Bancewicz
 
PPT
Jquery Best Practices
brinsknaps
 
Jquery-overview
Isfand yar Khan
 
jQuery from the very beginning
Anis Ahmad
 
D3.js and SVG
Karol Depka Pradzinski
 
jQuery
Vishwa Mohan
 
jQuery Essentials
Marc Grabanski
 
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
Drupal Best Practices
manugoel2003
 
Jquery introduction
musrath mohammad
 
History of jQuery
jeresig
 
jQuery Loves Developers - Oredev 2009
Remy Sharp
 
Learning jQuery in 30 minutes
Simon Willison
 
jQuery - Tips And Tricks
Lester Lievens
 
Introduction to jQuery
Zeeshan Khan
 
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
06 jQuery #burningkeyboards
Denis Ristic
 
jQuery Presentation
Rod Johnson
 
jQuery Introduction
Arwid Bancewicz
 
Jquery Best Practices
brinsknaps
 

Similar to Jquery optimization-tips (20)

PDF
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
KEY
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
PDF
jQuery
Ivano Malavolta
 
PDF
22 j query1
Fajar Baskoro
 
PDF
jQuery secrets
Bastian Feder
 
KEY
Introduction to jQuery - Barcamp London 9
Jack Franklin
 
PDF
Writing Maintainable JavaScript
Andrew Dupont
 
PPTX
J query1
Manav Prasad
 
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
PDF
jQuery secrets
Bastian Feder
 
KEY
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
PDF
DOM Scripting Toolkit - jQuery
Remy Sharp
 
PPTX
Javascript first-class citizenery
toddbr
 
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
PDF
Building Large jQuery Applications
Rebecca Murphey
 
PDF
Introduction to jQuery
Nagaraju Sangam
 
PPTX
Jquery Basics
Umeshwaran V
 
PDF
Game jump: frontend introduction #1
Sebastian Pożoga
 
PDF
Jquery presentation
Mevin Mohan
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
22 j query1
Fajar Baskoro
 
jQuery secrets
Bastian Feder
 
Introduction to jQuery - Barcamp London 9
Jack Franklin
 
Writing Maintainable JavaScript
Andrew Dupont
 
J query1
Manav Prasad
 
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
jQuery secrets
Bastian Feder
 
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
DOM Scripting Toolkit - jQuery
Remy Sharp
 
Javascript first-class citizenery
toddbr
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
Building Large jQuery Applications
Rebecca Murphey
 
Introduction to jQuery
Nagaraju Sangam
 
Jquery Basics
Umeshwaran V
 
Game jump: frontend introduction #1
Sebastian Pożoga
 
Jquery presentation
Mevin Mohan
 
Ad

Recently uploaded (20)

PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Ad

Jquery optimization-tips

  • 1. $ will do everything for us Naga HaRISH ShareOurIdeas.com
  • 2. Agenda $ Few words about jQuery $ Why we need it $ Optimization tips # Load jQuery # Using Selectors # And more …more worth is waiting…!
  • 3. $(jQuery) $ this is a new kind of JavaScript Library. $ It is a lightweight cross-browser (FF,GC,IE,OP,SF). $ And it is the most popular JS library in use today. $ It was released in 2006 1st Half and current version is 1.7.1 $.Next()
  • 4. Each( features ) $ LESS CODE DO MORE $ We can do Element Styling, Events Handling, DOM manipulation, Animations and Ajax. $ We can develop site rapidly. Because it was ready with cool stuff. $ It is most popular and many awesome plugins. $ Having good Support and Documentation.
  • 5. $(“.Tips”) - Load jQuery Script $ Load minified file, because it small in size (31k < 229k) $ It is best to use Content Delivery Network(CDN) URL $ Let’s Google or Microsoft host jQuery file for U # Google src http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js # Microsoft src http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js $ Forget About this if you work for local apps
  • 8. jQuery.noConflict() $ Use no conflict method when you using other Libraries. $ Eg:- jQuery.noConflict() jQuery(“#Slide6”).next() (Or) var $j=jQuery.noConflict() $j(“#Slide6”).next()
  • 10. Start method $ Best to use jQuery ready method. $ Because we can use it more times, where javascript page onload is only once. $(document).ready(function() { //write your code here } (or) $(function(){ //write your code here });
  • 11. DEMO
  • 12. Optimize performance, using selectors $ Optimize selectors to descend from an ID, if possible. E.g:- $(“#id ul li”).hide() $ Use tag names when selecting classes E.g:- $(“p.class”).show() $ We can use comma(,) for multiple selectors E.g:- $(“p, ul li,div).css(‘color’,’#000’) More here http://api.jquery.com/?s=selector
  • 13. DEMO
  • 14. Use Chaining $ Take advantage of Chaining $ Apply multiple effects and DOM manipulation @ a time Without Chaining With Chaining $(document).ready(function () { $(document).ready(function () { $('#id').css('color', #143'); $('#id').css('color', $('#id').html(' Request'); #200').html(' Response') $('#id').click(function () { .click(function () { //something //something }); }); }); }); “.css(), .html(…) and more” functions return a jQuery object that can be worked on instantly. So we use it right away to set the other properties like click event handler.
  • 15. DEMO
  • 16. Use Caching $ Caching is a great way to limit the number of times we need to traverse the DOM to find elements matched by the selectors. Without Caching With Caching $(document).ready(function () { $(document).ready(function () { $('#id').css('color', #143'); var id=$('#id'); : id.css('color', #143'); If(isOk) : $('#id').html(' Request'); If(isOk) : id.html(' Request'); If(isEnable) : $('#id').click(function () { If(isEnable) //something id.click(function () { }); }); //something }); });
  • 17. DEMO
  • 18. Click vs Bind vs Live $ Click is to handle click event on element $('#target').click(function() { alert('Handler for .click() called.'); }); $ Bind is used to like same way, to add events. But not only click event. We can create our custom events too. $('#foo').bind('click', function() { $(this).toggleClass('entered'); }); or $('#foo').bind('valid', function() { //Todo}); and we can fire this event like this $(‘#foo’).bind()
  • 19. Click vs Bind vs Live $ Live, will work same like Bind. $ But, Bind can’t add event handler to run time add elements. $ Live will automatically adds handlers for new elements.
  • 20. Event delegation $ Bind handler only once to the parent element containing all the children meant to have handlers. Using Event delegation Normal way $(document).ready(function () { $(document).ready(function () { $('ul').delegate('click', 'li', function (e) { $('ul li').click(function () { // if ($(e.target).is('li')) $(this).toggleClass('hig $(e.target).toggleClass('highlight'); hlight'); }); }); }); }); $ Advantage of this is it will automatically bind click event for new(runtime add) element too.
  • 21. DEMO
  • 22. Minimize DOM manipulation $ We have to minimize DOM operations (.append() .prepend() .after() .wrap() and ..) Without temp variable (full Without temp variable :- DOM):- var myNumbers = $('#Numbers'); var myNumbers = var myString= ''; $('#Numbers'); for (i = 0; i < 1000; i++) { for (i=0; i<1000; i++){ mystring += '<li>Number ' + i + '</li>'; myNumbers.append('Number } ' + i); } myList.html(myNumbers);
  • 23. Minimize DOM manipulation(cont..) $ Use template for array items $ !dea from Microsoft $ jQuery add in documentation $ More http://api.jquery.com/jQuery.template/ $ http://api.jquery.com/jquery.tmpl/
  • 24. DEMO
  • 25. Use $(this) and this $ $(this) is jQuery object $ this is traditional HTML object $(document).ready(function () { $('ul li').click(function () { $(this).toggleClass('highlight'); alert(this.innerHTML); }); });
  • 26. DEMO
  • 27. Use data $ Useful. It allows you to bind data to DOM elements without modifying the DOM. $ For example:- # $("div").data("message", "Done!"); # $("div").data("message"); OR # <div data-error=“Error! :(“>…</div> # $("div").data("error");
  • 28. DEMO