Some of them are really popular. For instance my Bulk Delete WordPress Plugin has been downloaded for nearly 100,000 times. The flip side of it is that these pet projects have some additional baggage as well. Support questions, regular updates, feature requests etc can be sometimes very time consuming.
I always wanted to know the projects, which takes up most of my free time, but didn’t had any metrics or logs. So I have started to track the amount of time I am spending on each of these projects.
The advantage of this is that, now I know which project are real time hogs and can have some metrics to decide on whether it is worth spending time on those projects or not. I am also going to add this data to the project readme files, so that people who use it might also know the amount of time and effort I have put it on those projects š
I thought it would be nice to have a wrapper like that for jQuery, which led to this nice little Plugin for jQuery called jQuery Later
Download
You can download it from my github page. It also includes a html page which tells you how to use it.
Syntax
jQuery later function follows the same syntax as that of YUI.
jQuery.later ( when , o , fn , data , periodic )
Executes the supplied function in the context of the supplied object ‘when’ milliseconds later. Executes the function a single time unless periodic is set to true.
Parameters:
when <int> the number of milliseconds to wait until the fn is executed.
o <object> the context object.
fn <Function|String> the function to execute or the name of the method in the ‘o’ object to execute.
data <object> [Array] data that is provided to the function. This accepts either a single item or an array. If an array is provided, the function is executed with one parameter for each array item. If you need to pass a single array parameter, it needs to be wrapped in an array [myarray].
periodic <boolean> if true, executes continuously at supplied interval until canceled.
Returns: object
a timer object. Call the cancel() method on this object to stop the timer.
Well sometime back, I faced this unique problem. It took me sometime to solve it, so I thought of documenting it as my learning so that it is useful for others and might help me one when I need to do it again.
Problem
Okay, the following is the requirement. You have a web page with a set of text boxes. The number of text-boxes might vary based on some backend logic. You need to make sure the user doesnāt enter duplicate values in these set of textboxes. He can leave them blank, but cannot enter duplicate values.
Solution
The following is my solution.
function findDuplicates() {
var isDuplicate = false;
jQuery("input[name^='access_keys']").each(function (i,el1) {
var current_val = jQuery(el1).val();
if (current_val != "") {
jQuery("input[name^='access_keys']").each(function (i,el2) {
if (jQuery(el2).val() == current_val && jQuery(el1).attr("name") != jQuery(el2).attr("name")) {
isDuplicate = true;
jQuery(el2).css("background-color", "yellow");
jQuery(el1).css("background-color", "yellow");
return;
}
});
}
});
if (isDuplicate) {
alert ("Duplicate values found.");
return false;
} else {
return true;
}
}
I basically loop through all the text boxes and compare the values with all the other textboxes, in nested loops. Basically it is like bubble sort. I know this is bad, but was not able to think of any efficient way of doing it. Do you guys can think of a better way?
I just finished watching John Resigās talk titled “Advanced jQuery” which he gave at Ajax Experience. Like I said before, I am posting notes which I took while watching the video, so that I can refer to them at a later point of time (It is easier to search when it is not on paper š ). Also it might help someone to get the outline of the talk before actually watching the video. The original video runs for more than an hour.
Also be warned that the following is my own interpretation of the video and I might have missed or could have interpreted some point differently. š
About the speaker, John Resig
As you all know John Resig is the creator of the excellent jQuery library. He works for Mozilla corporation and you can get more information about him from his blog.
Some Advanced features of jQuery
Chaining
The most useful and the powerful feature of jQuery. (Personally this is my favorite feature in jQuery). Every function in jQuery returns the jQuery object and you can continue to call functions on that object. end() stops chaining andSelf() adds the current element into the chain.
Isolation
Once of the design goal of jQuery is that, jQuery should not affect or get affected on any HTML page, no matter what is present in the HTML page, either another library or even another version of jQuery itself. The only exception is when a new property is added to object.prototype.
Isolation in jQuery is achieved by the following
jQuery Wrapper
Everything in jQuery is wrapped. Only the $ function and the jQuery variable are exposed.
Plugin Wrapping
All Plugin code should be wrapped between
(function($) {
})(jQuery);
noConflict
Even the global jQuery variable can be unrolled by using jQuery.noConflict(true). This allows two versions of jQuery to run on a single HTML document without any conflict.
Sizzle
Sizzle is a selector engine written by John in pure JavaScript. It is around 1.5x to 4x faster and the major advantage of sizzle is that it can be integrated with other libraries, since it doesnāt have any dependency on jQuery.
Sizzle works from backwards while selecting and is therefore faster in most cases. It also caches the DOM tree and frees it by listening to the DOMAttrModified, DOMNodeInserted and DOMNodeRemoved events.
Sizzle has a separate homepage and you can get more information from itās homepage.
DOM Manipulation
By far, most time of a script execution is spent only in DOM Manipulation.
jQuery automatically converts single tags to double tags using the following function. (I never know this before. The function seems to be very precise and well thought of)
elem = elem.replace(/(<(\w+)[^>]*?)\/>/q, function (all, front, tag) {
return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? all : front + "></" + tag + ">";
}
jQuery 1.3 uses document Fragment for DOM manipulation and is much faster when compared with older versions.
Events
In jQuery you can bind event to any JavaScript Object.
Namespaced Events
From jQuery 1.2, Plugins can use namespaced events. The major advantage in using name spaced events is that it is very easy to clean up later and uses less memory space.
Special Events
From jQuery 1.2.2 some special events are available for easy event handling.
Events like mouseEnter, mouseLeave and mouseWheel are not actual events but are exposed by jQuery. jQuery does the internal implementation for these events.
Video
So my dear readers what you think about my notes. Also let me know if you like notes for videos in this format. If there is a demand, then I can post some of my notes on other videos which I have already viewed.
Regular readers of this blog will know that I am great fan of jQuery. Recently I am thinking of evangelizing jQuery at work and convince people who take up decision to make jQuery as the default JavaScript framework to be used across the organization.
The following is the list of advantages/benefits which I have prepared so far.Ā
Light weight and has a very small footprint.
Browser abstraction – jQuery provides browser abstraction and is hightly optimized for each individual browser.
Excellent Plugin architecture – jQuery has an excellent Plugin architecture and you can find a jQuery Plugin for anything that you want to do.
I just finished viewing John Resigās talk titled “DOM is a mess” at Yahoo. I took some notes while watching the video and I am posting them here, so that I can refer to them at a later point of time (It is easier to search when it is not on paper š ). Also it might help someone to get the outline of the talk before actually watching the video. The original video runs for more than an hour.
Also be warned that the following is my own interpretation of the video and I might have missed or could have interpreted some point differently. š
About the speaker, John Resig
As you all know John Resig is the creator of the excellent jQuery library. He works for Mozilla corporation and you can get more information about him from his blog.
DOM is a mess
This is the first thing John Resig said about DOM methods after saying that DOM is a messy
Nearly every DOM method is broken in some way, in some browser.
The following are some of the bugs in the DOM methods
getElementByID ()
IE and Old versions of Opera return elements whose name == id
getElementByTagName ()
.length gets overwritten in IE if an element with an ID = ālengthā is found
getElementsByClassName ()
Opera doesnāt match a second specified class
querySelectorAll ()
Safari 3.2 canāt match uppercase characters in quirks mode.
So the moral is that almost every method in DOM is messed up.
Writing Cross-browser code
Find out the cost/benefit ratio for supporting a browser and then pick the browsers you are going to support before writing your code.
So my dear readers what you think about my notes. Also let me know if you like notes for videos in this format. If there is a demand, then I can post some of my notes on other videos which I have already viewed.
Out of the numerous JavaScript libraries which are available, jQuery is my favourite. There are couple of reasons for this and now I have two more reasons added to that list.Ā
I found a lot of useful links related to jQuery while researching for my presentation on jQuery. I thought of sharing them here so that it will be useful for others who are looking to get their feet wet with jQuery.
I am planning to constantly updated this post, so if you have any other links which might be useful do leave a comment and I will add them.
I attended WebCamp today as planned. It was good and there were talks about web frameworks like Ruby on Rails, Groovy on Grails, Django, Flex etc. Unfortunately I was not able to give my talk about jQuery as planned due to some issues with the timings. I was pretty disappointed at first; since it took me considerable amount of time to prepare for the presentation and added to that I drove for more than two hours to attend the event. Anyway that’s life isn’t? š You will never know what life has for you the next second. If everything goes as planned, then life would be as dull as a recorded circket match. It is uncertainty which gives life to life (wow! I can make a quote too š ).
Anyways to be positive I learned a lot of new things about jQuery when I did my research to prepare for the presentation, even though I have been using it for the past couple of months. Hope you guys also find my slides about jQuery useful.