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
Ad

More Related Content

What's hot (20)

jQuery
jQueryjQuery
jQuery
Mohammed Arif
 
Jquery-overview
Jquery-overviewJquery-overview
Jquery-overview
Isfand yar Khan
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
 
D3.js and SVG
D3.js and SVGD3.js and SVG
D3.js and SVG
Karol Depka Pradzinski
 
jQuery
jQueryjQuery
jQuery
Vishwa Mohan
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
Marc Grabanski
 
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
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
manugoel2003
 
Jquery introduction
Jquery introductionJquery introduction
Jquery introduction
musrath mohammad
 
History of jQuery
History of jQueryHistory of jQuery
History of jQuery
jeresig
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
Remy Sharp
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
Simon Willison
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And Tricks
Lester Lievens
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Zeeshan Khan
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
Denis Ristic
 
jQuery
jQueryjQuery
jQuery
Mostafa Bayomi
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
Rod Johnson
 
jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
Arwid Bancewicz
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
brinsknaps
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
 
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
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
manugoel2003
 
History of jQuery
History of jQueryHistory of jQuery
History of jQuery
jeresig
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
Remy Sharp
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
Simon Willison
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And Tricks
Lester Lievens
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Zeeshan Khan
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
Denis Ristic
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
Rod Johnson
 
Jquery Best Practices
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
brinsknaps
 

Similar to Jquery optimization-tips (20)

jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
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
Jonas De Smet
 
jQuery
jQueryjQuery
jQuery
Ivano Malavolta
 
J query training
J query trainingJ query training
J query training
FIS - Fidelity Information Services
 
22 j query1
22 j query122 j query1
22 j query1
Fajar Baskoro
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
Bastian Feder
 
Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9
Jack Franklin
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
Andrew Dupont
 
J query1
J query1J query1
J query1
Manav Prasad
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
Bastian Feder
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
Remy Sharp
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
toddbr
 
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
Guy Royse
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery Applications
Rebecca Murphey
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Nagaraju Sangam
 
Jquery Basics
Jquery BasicsJquery Basics
Jquery Basics
Umeshwaran V
 
Game jump: frontend introduction #1
Game jump: frontend introduction #1Game jump: frontend introduction #1
Game jump: frontend introduction #1
Sebastian Pożoga
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
Mevin Mohan
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
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
Jonas De Smet
 
Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9Introduction to jQuery - Barcamp London 9
Introduction to jQuery - Barcamp London 9
Jack Franklin
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
Andrew Dupont
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
Remy Sharp
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
toddbr
 
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
Guy Royse
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery Applications
Rebecca Murphey
 
Game jump: frontend introduction #1
Game jump: frontend introduction #1Game jump: frontend introduction #1
Game jump: frontend introduction #1
Sebastian Pożoga
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
Mevin Mohan
 
Ad

Recently uploaded (20)

Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdfAutomate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Precisely
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
The Microsoft Excel Parts Presentation.pdf
The Microsoft Excel Parts Presentation.pdfThe Microsoft Excel Parts Presentation.pdf
The Microsoft Excel Parts Presentation.pdf
YvonneRoseEranista
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdfAutomate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Automate Studio Training: Building Scripts for SAP Fiori and GUI for HTML.pdf
Precisely
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
The Microsoft Excel Parts Presentation.pdf
The Microsoft Excel Parts Presentation.pdfThe Microsoft Excel Parts Presentation.pdf
The Microsoft Excel Parts Presentation.pdf
YvonneRoseEranista
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
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