~~SLIDESHOW~~ ====== JavaScript Libraries and jQuery ====== **Contact Hour 14**: To be discussed on Wednesday 27th February, 2013. **Presenter**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. As a way of getting around browser quirks, the web developer community, including some major players like Google and Yahoo, have developed and released JavaScript libraries. In this session we will explore the idea of a JavaScript library, mention some of the general purpose and specialized open-source libraries that exist, and introduce jQuery, one of the most widely used JavaScript libraries. ===== JavaScript Libraries ===== * JavaScript is a small language * It doesn't come with large numbers of libraries and objects * Differences between browsers make JavaScript DOM programming challenging * JavaScipt can be used to solve these problems itself * Solutions to common problems have been shared as open source JavaScript libraries ---- JavaScript is a small language that can be covered in about two weeks worth of lectures if you already know a c-like programming language. Unlike technologies like .NET, Java and Cocoa, it doesn't come with large numbers of libraries and objects that we can use to develop web applications. Unfortunately, even for the limited requirement of manipulating the DOM, differences between browsers make JavaScript more challenging than it should be. Fortunately, many of the problems can be solved using JavaScipt itself, and even better, solutions have been made into open source libraries that have been shared on the Internet. This material, including some of the examples, is based in part on Chapter 11 of Simon Allardice, //JavaScript Essential Training (2011)//, __lynda.com__, 22 July 2011. URL: [[http://www.lynda.com/JavaScript-tutorials/Essential-Training-2011/81266-2.html]] accessed 18 February 2012 and Joe Marini, //Essential jQuery Training//, __lynda.com__, 9 January 2009. URL: [[http://www.lynda.com/jQuery-tutorials/essential-training/48370-2.html]] accessed 18 February 2012. For a free alternative see Buck Rogers, //jQuery Tutorial (200 videos)//, __thenewboston.com__, URL: [[http://thenewboston.org/list.php?cat=32]], last accessed 18 February 2012. Other examples are taken from public sources on the Internet which are cited in the notes. For a previous version of these notes see [[eg-259:lecture13|Old JavaScript Libraries]] notes. ===== Contents of this Session ===== * [[eg-259:ch14#General_Purpose_JavaScript_Libraries|General Purpose JavaScript Libraries]] * [[eg-259:ch14#Special_Purpose_JavaScript_Libraries|Specialized JavaScript Libraries]] * [[eg-259:ch14#MVC_JavaScript_Libraries|MVC JavaScript Libraries]] * [[eg-259:ch14#Using_Libraries|Using Libraries]] * [[eg-259:ch14#jQuery|jQuery]] * [[eg-259:ch14#Example|Example]] * [[eg-259:ch14#jQuery_UI Components|jQuery UI Components and jQuery Mobile]] * [[eg-259:ch14#Using_CDN|Using CDN]] ===== Learning Outcomes ==== //At the end of this lecture you should be able to answer these questions//: - What features are typically provided by a //general purpose// JavaScript library? - What features are typically provided by a //specialized// JavaScript library? - How is a JavaScript library typically loaded? - Why is is recommended that most scripts and libraries are loaded at the end of an HTML document? ===== Learning Outcomes (2) ==== //At the end of this lecture you should be able to answer these questions//: - List the main sections of the jQuery API - What is the difference between //selection// and //filtering// in jQuery? - Would it be a good idea to use jQuery's CSS selection as a replacement for style sheets? - What are the two ways to assign an event handler to an element with jQuery? - What attributes and methods does the jQuery event object have? ===== Learning Outcomes (3) ==== //At the end of this lecture you should be able to answer these questions//: - What does the jQuery function return? - How does the object returned by the jQuery function allow chaining of functions? - Give an example of jQuery's animation method. - What widgets does the standard JQuery UI library contain? - What advantages does jQuery mobile offer the developer of mobile web apps? - Why is jQuery code usually embedded in an anonymous function passed as a parameter to the ''jQuery("document").ready()'' function? - What advantage does loading a JavaScript library from a CDN have? ===== General Purpose JavaScript Libraries ===== * [[http://code.google.com/closure/library/|Google's Closure Library]] * [[http://mootools.net/|MooTools]] * [[http://developer.yahoo.com/yui/|Yahoo! YUI Library]] * [[http://dojotoolkit.org/|dojo]] * [[http://www.prototypejs.org/|Prototype]] * [[http://jquery.com/|jQuery]] ===== Typical Features ===== * Navigating the DOM * retrieving elements * modifying element attributes or contents * putting modified elements back into DOM * Cross-browser event handling * Cross-browser animation * Support for AJAX ===== Specialized JavaScript Libraries ===== For specialized features: * [[http://www.lokeshdhakar.com/projects/lightbox2/|Lightbox2]] -- image display * [[http://script.aculo.us/|Script.aculo.us]] -- visual effects, animation and UI controls * [[http://moofx.mad4milk.net|moofx]] -- visual effects * [[http://www.curvycorners.net/|CurveyCorners]] -- rounded corners on browsers that don't support CSS3 * [[http://code.google.com/p/webforms2/|webforms2]] -- cross-browser support for full WebForms 2.0 spec (see [[eg-259:lecture11|HTML Forms the Next Generation]]) * [[http://www.tinymce.com/|TinyMCE]] -- adds Word toolbar-like features and WYSIWYG editing to text areas on web forms ---- Some JavaScript libraries are more specialized. Rather than be general purpose, things like Lightbox are simply JavaScript libraries for popping up images on your web site. You've probably seen these ones before. They might be for slideshows. They might be for implementing things like drag-and-drop and working with accordions. They could be as simple as a library that just helps you add curved corners to your divs. And some, might be used as //polyfills// that make standards that are not yet implemented in browsers -- such as the full web forms 2 standard -- or back-port features such as those introduced in HTML5 to older browsers. Most of these libraries are open source and they are free. There is no licensing. You simply grab the code and use it, do whatever you want with it. So we encourage you to explore what's available, and new ones are appearing all the time. ===== MVC JavaScript Libraries ===== * JavaScript frameworks for rich client-side interaction * Designed to separate code for the presentation of data (Model) in DOM (View) from the model (Application) data itself. * Controllers for coordination of data flow, state management, routing, browser history control, etc. * For now see collection in [[http://addyosmani.github.com/todomvc/|TodoMVC]] To be discussed in a later session. ===== Using Libraries ===== * Simply download * Include in document using ''
According to current best practice, you should load your scripts last.
Order is important!