Search This Blog

Showing posts with label jQuery For Beginners. Show all posts
Showing posts with label jQuery For Beginners. Show all posts

Thursday, 20 June 2013

Use protocol less URL for referencing jQuery

Learnt something new today and thought of sharing. Most of us, include the reference of jQuery library (if Google CDN is used) like this,
http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
As you can see it is using HTTP protocol. The advantage of Google CDN files are cached up to one year which means that your users may not need to download jQuery at all. But when you move from HTTP to HTTPS pages, then you need to make sure that you are using https protocol for referencing jQuery as pages served via SSL should contain no references to content served through unencrypted connections.

Related Post:
And take a note that content served via SSL are not cacheable. Browsers will simply ignore content served over SSL for caching. So you need to make sure that if you are moving from HTTP to HTTPS url, use correct protocol for referencing jQuery library as well.

The better approach would be to use "protocol-less" URL.
//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
It looks strange and initially I also felt the same but "protocol-less" URL is the best way to reference third party content that’s available via both HTTP and HTTPS. When a URL’s protocol is omitted, the browser uses the underlying document’s protocol instead.

Thus, using the protocol-less URL allows a single script reference to adapt itself to what’s most optimal: HTTP and it’s full caching support on HTTP pages, and HTTPS on secured pages so that your users aren't confronted with a mixed content warning.

Must Read:
Feel free to contact me for any help related to jQuery, I will gladly help you.

Tuesday, 4 June 2013

jQuery .end() and Chaining

Before you go further, I assume that you know What is chaining in context of jQuery? If not, then first read "What is Chaining in jQuery?". To summarize Chaining in jQuery means to connect multiple functions, events on selectors. Main advantage of using chaining is that it makes code look clean and gives better performance. You should make a habit of using chaining.

Related Post:

You will find that chaining is very easy to implement and indeed it is. But consider a scenario, where in a <div> element, find all <p> and <span> elements and assign background color to them. Below jQuery code will exactly do the needful.
$(document).ready(function () {
$('div').find('p').css("background", "yellow");
$('div').find('span').css("background", "pink");
});
Can this be done using chaining? The first line of code $('div').find('p'), will select all the <p> tag element in <div> and change background color. Since using find() we have filtered out all <p> elements so we need to use selector again to select <span>tag element and we can't use chaining. Fortunately, jQuery has .end() function which end the most recent filtering operation in the current chain and return to its previous state. Like,
$(document).ready(function () {
$('div')
.find('p').css("background", "yellow")
.end()
.find('span').css("background", "pink");
});
This chain searches for <p> tag element in <div> and turns their backgrounds yellow. Then .end() returns the object to its previous state which was before the call to find(), so the second find() looks for <span> inside <div>, not inside <p>, and turns the matching elements' backgrounds to pink. The .end() method is useful primarily when exploiting jQuery's chaining properties.
See Complete Code
Feel free to contact me for any help related to jQuery, I will gladly help you.

Sunday, 26 May 2013

Free jQuery Courses and Training material


In this post, we bring you a list of Free jQuery Course and Training Material which are useful from beginner to an expert. Of course, this blog has some useful, handy material but the below list has some serious and exceptional training materials which are created specifically for training purpose.


  • Try jQuery. Try jQuery walks you through the most fundamental building blocks of jQuery, from actually getting the library into your page to selecting, manipulating, and creating DOM elements, and reacting to user input.

  • Learn jQuery is the official learning portal for the library. If you're looking for explanations of the basics, workarounds for common problems, best practices, and how-tos, then that's the right place!

  • jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery.

  • Lessons by appendTo() has collection of free video lessons on JavaScript, jQuery, events, methods and selectors.

  • Learn jQuery in 30 Days is a free newsletter course by tutsplus. Once you subscribe, each day you'll be sent a free video lesson in your email for 30 days.

  • jQuery Succinctly was written to express, in short-order, the concepts essential to intermediate and advanced jQuery development. Its purpose is to instill in you, the reader, practices that jQuery developers take as common knowledge.


Feel free to contact me for any help related to jQuery, I will gladly help you.

Wednesday, 17 April 2013

Direct vs Delegated Events with jQuery on() method

jQuery added a new method called  on()  in release 1.7 which provides all functionality for attaching event handlers. And it has made other event handler event like live() and delegate() dead. This method was introduced due to performance issue with live() method.

Related Post:

There are 2 signaturs of  on()  method.
  1. .on(events[,selector] [,data ],handler(eventObject))

  2. .on(events[,selector] [,data])

In this post, we will focus on the one of the optional parameter named "selector". The official jQuery documentation about this parameter says,

" A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element."

As this is an optional parameter, so it can be omitted. The present/absence of this parameter defines Direct Event or Delegated Event. If it is absent, then it is called "Direct event", otherwise "Delegated event". First, we will see what Direct event means.

For example, there are 2 div element present on the page,
<div>First Div</div>
<div>Second Div</div>
And attach a click event on div element using on() method, which just alerts the clicked element text.
$(document).ready(function () {
$('div').on('click', function() {
alert("Clicked: " + $(this).html());
});
});
So the above jQuery code instructs "HEY!!!! I want every div to listen to click event. And when you are clicked , give me alert." Now, this is absolutely fine and works like charm. This is called "Direct Event". But there is an issue with Direct events. Official jQuery document about Direct events says,

" Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on()."

So for Direct events to work, the element has to present on the page. If it is added dynamically, it's not going to work.

Consider the same example, which now on click event adds a div element dynamically, after showing alert. But the click event don't get attached to dynamically added elements.
$(document).ready(function () {
$('div').on('click', function() {
alert("Clicked: " + $(this).html());
$('<div>Dynamic Div</div>').appendTo('body');
});
});
So when dynamically added div elements are clicked, nothing happens! As the event is not attached to them.
So, what is the solution now? Well, solution is to use "Delegated events". Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

When a selector is provided, the event handler is referred to as delegated. So let's modify the above jQuery code to have "Delegated event" instead of "Direct event".
$(document).ready(function () {
$(document).on("click","div", function(){
alert("Clicked: " + $(this).html());
$('<div>Dynamic Div</div>').appendTo('body');
});
});
So the jQuery code instructs "HEY!! document when your child element which is div gets clicked, give me alert and append a dynamic div to body." Using delegated event, you will find the same behavior for already present and dynamically added elements.
There is another advantage of using delegated event which is "much lower overhead". For example, On a table with 1,000 rows in its tbody, below code attaches a handler to 1,000 elements.
$("#Table tbody tr").on("click", function(event){
alert($(this).text());
});
On the other side, using "Delegated event", attaching event handler to only one element which is tbody. The event only needs to bubble up one level (from the clicked tr to tbody).
$("#Table tbody").on("click", "tr", function(event){
alert($(this).text());
});
Let's take another example, to explain "Delegated events". Consider following Html code.
<div id="container"> 
<span> Span within Container div. </span>
<br/> <span> Another span within Container div. </span>
</div>
And below is jQuery code to attach (delegated) click event on span element within div with ID "container" is,
$(document).ready(function () {
var iCount = 1;
$('div#container').on("click", "span", function () {
alert("Clicked: " + $(this).html());
var $dynSpan = $('<br/><span> Dynamic Span ' + iCount + '.</span>');
$dynSpan.appendTo('div#container');
iCount++;
});
});
Here is another important tips "Attaching many delegated event handlers near the top of the document tree can degrade performance". For best performance, attach delegated events at a document location as close as possible to the target elements as done in above jQuery code. So instead of $(document).on("click","div#container span", function(){ , use $('div#container').on("click", "span", function () {

Feel free to contact me for any help related to jQuery, I will gladly help you.