1
CS428 Web Engineering
Lecture 08
JavaScript Library
Today’s Goal:
Jquery & Ajax
• To become able to appreciate the concept of
JavaScript libraries including JQuery:
– What are they?
– What do they do?
– How do we benefit from them?
3
JavaScript Libraries(1)
• JavaScript is really a small language.
• But unlike technologies like C++, Java, .Net etc
it doesn’t have dozens of built-in libraries with thousands of objects to use.
• A lot of people write code in JavaScript and
bundled it together into one of the form of
JavaScript Libraries(2)
• JavaScript libraries simply means the
JavaScript code that someone else wrote and you don’t have to.
• So, what are these libraries, they are dozens of
them, we just used few
• Some of these libraries like google closure,
JavaScript Libraries(3)
• They have whole lots of function that helps navigating to the DOM, they include code for
cross browser detection. Easier to work with
events without worrying which version of
Internet Explorer you might using or working with animations.
• Make it easy to have your JavaScript request
11
• To use any of these libraries I just have to go
there and downloaded.
• For Example: www.jquery.com
• You just download it and link it like you link your
• Now on other hand some JavaScript libraries
are more specialized rather then the general purpose.
• Example: www.huddletogether.com LightBox
are simple JavaScript libraries for popping up images on your website.
• They might be use for implementing things like
15
• They help you to add curve corners etc
• Most of these libraries are open source and they
are free, No licensing
• You simply grab the code and use it.
• The most general purpose JavaScript library is
JQuery. Knowing the JavaScript library particularly JQuery is core skill for a JavaScript developer
What is JQuery?
• JQuery is a lightweight,”write less, do more”
JavaScript library.
• The purpose of jQuery is to make it much
easier to use JavaScript on your website.
• How to use it?
• Visit www.jquery.com and download both
Downloading JQuery
• There are two versions of jQuery available for
downloading:
– Production version - this is for your live website
because it has been minified and compressed
– Development version - this is for testing and
development (uncompressed and readable code)
• Both versions can be downloaded from
jQuery.com.
jQuery Environment Setup
<!DOCTYPE html>
<html lang=‘en’> <head>
<title>Title of page</title>
<script type="text/javascript"
src="/js/jquery-2.1.3.min.js"></script>
</head>
<body>
This is my first homepage. <b>This text is bold</b>
</body>
</html>
jQuery CDN
• If you don't want to download and host jQuery yourself, you can include it
from a CDN (Content Delivery Network).
• Both Google and Microsoft host jQuery.
• Google CDN:
• <head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></ script>
</head>
• Microsoft CDN:
• <head>
21
THE DOCUMENT READY EVENT
• $(document).ready(function(){ // jQuery methods go here...
});
• This is to prevent any jQuery code from running before
the document is finished loading (is ready).
• It is good practice to wait for the document to be fully
JQUERY SYNTAX
• Basic syntax is: $(selector).action()
• A $ sign to define/access jQuery
• A (selector) to "query (or find)" HTML elements • A jQuery action() to be performed on the
23
EXAMPLES
• $(this).hide() - hides the current element.
• $("p").hide() - hides all <p> elements.
• $(".test").hide() - hides all elements with
class="test".
• $("#test").hide() - hides the element with
JQUERY SELECTORS
• jQuery selectors allow you to select and
manipulate HTML element(s).
• With jQuery selectors you can find elements
based on their id, classes, types, attributes,
values of attributes and much more. It's based
on the existing CSS Selectors, and in addition, it has some own custom selectors.
25
THE ELEMENT SELECTOR
• The jQuery element selector selects elements
based on their tag names. You can select all <p> elements on a page like this:
• Example:
26
THE #ID SELECTOR
• The jQuery #id selector uses the id attribute of
an HTML tag to find the specific element.
• To find an element with a specific id, write a
hash character, followed by the id of the element:
• Example:
27
THE
.
CLASS SELECTOR
• The jQuery class selector finds elements with a
specific class.
• To find elements with a specific class, write a
period character, followed by the name of the class:
• Example:
28
MORE EXAMPLES OF SELECTOR
Syntax Description
• $("*") Selects all elements
• $(this) Selects the current HTML
element
• $("p.intro") Selects all <p> elements with
class="intro"
• $("p:first") Selects the first <p> element • $("[href]") Selects all elements with an
href attribute
REGULAR JavaScript vs. JQuery
• Without JQuery we often write lines of code like this:document.getElementById(“myDiv”).className = “highlight”;
jQuery(“#myDiv”).addClass(“highlight”);
there is two reason why JQuery code is preferable:
1) getElementById(“myDiv”) we must have an ID “myDiv” in HTML, if we doesn’t have ID “myDiv” we can’t get to it. But JQuery is much more flexible
2) We can give JQuery ID to find using hash sign, but we can also find other parts of the page that we can’t get
element by ID. For Example: we can get all the elements belong to a particular class
jQuery(“.someClass”)
jQuery(“p”) jQuery(“a”)
jQuery(“li”)
• jQuery(“#myDiv”).addClass(“highlight”);
• We are telling jQuery what we wanted to find
• jQuery(“p.description”)
• Give me all the paragraph that have a class
• Further words we can add to further refine
our jQuery statement.
:first :last
:contains() :visible
JQUERY METHODS
• $( “what to find” ).someAction;
• $( “#myDiv” ).addClass( “highlight” );
.removeClass( “highlight” ); .toggleClass( “highlight” );
Note:
JQUERY ALIAS
• jQuery( “#myDiv” ).addClass( “highlight” );
• $( “#myDiv” ).addClass( “highlight” );
• General Format:
• $( “what to find” ).someAction(any params);
What is AJAX?
•
AJAX stands for Asynchronous JavaScript AndXML
•
Allows web pages or parts of them to beupdated asynchronously
•
Based on XML HTTP request object aka ( XHR)•
Requires basic understanding of:•
(X)HTML – displaying the data/Dynamic content•
CSS – styling the data•
JavaScript – manipulating the DOMAjax -
Usage samples
•
Autocomplete search textboxes ( sample)•
Cascading dropdown list boxes (sample )•
Real-time - Continuous data refresh (long polling, chat systems…)•
Immediate forms validation feedback ( sample)•
Conditional display / dynamic content ( sample)•
Auto save user information (Google Docs, Facebook)•
Ratings, voting & other instant actions (sample)Advantage of Ajax
•
Better interactivity & responsiveness•
Impressive UX•
Reduced connections to the web server(partial rendering)