10 JQuery Snippets For Efficient Developers

Posted: February 20, 2010 in JavaScript
Tags: , ,
jQuery is by far my favorite Javascript framework, which allows developers to create stunning visual effects, manipulate data properly, and much more. In this article, I have compiled 10 extremely useful jQuery snippets.

Load jQuery from Google

Google has a fresh version of jQuery, which is made available for developers. Instead of getting a jQuery copy, you should definitely take advantage of Google’s “generosity” and directly load their copy:

1.<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"type="text/javascript"></script>

» Source

****************************************************

Validate a date of birth using jQuery

Validating dates of birth are a common task on websites that have content available only for users 18+ years old. Using jQuery, this is very easy to do, as shown in the following example:

01.$("#lda-form").submit(function(){
02.var day = $("#day").val();
03.var month = $("#month").val();
04.var year = $("#year").val();
05.var age = 18;
06.var mydate = new Date();
07.mydate.setFullYear(year, month-1, day);
08.
09.var currdate = new Date();
10.currdate.setFullYear(currdate.getFullYear() - age);
11.if ((currdate - mydate) < 0){
12.alert("Sorry, only persons over the age of " + age + " may enter this site");
13.return false;
14.}
15.return true;
16.});

» Source

Make sure an image has loaded properly

How do you know if an image has been properly loaded? In some particular cases such as a captcha, problems with the user experience may happen if an image hasn’t been loaded properly.
Using the simple piece of code below, you’ll be able to know if your image is displayed on the user screen.

1.$('#myImage').attr('src', 'image.jpg').load(function() {
2.alert('Image Loaded');
3.});

» Source

XHTML 1.0 Strict valid target=”blank” attribute

The target=”blank” attribute can be useful when you want a link to be opened in a new tab or window. Though, the target=”blank” attribute is not valid XHTML 1.0 Strict.
using jQuery, you can achieve the same functionality without having validation problems.

1.$("a[@rel~='external']").click( function() {
2.window.open( $(this).attr('href') );
3.return false;
4.});

» Source

Search within text using jQuery

The following function will allow full text search on the page using jQuery. The feature is not only cool, but useful at the same time.

01.$.fn.egrep = function(pat) {
02.var out = [];
03.var textNodes = function(n) {
04.if (n.nodeType == Node.TEXT_NODE) {
05.var t = typeof pat == 'string' ?
06.n.nodeValue.indexOf(pat) != -1 :
07.pat.test(n.nodeValue);
08.if (t) {
09.out.push(n.parentNode);
10.}
11.}
12.else {
13.$.each(n.childNodes, function(a, b) {
14.textNodes(b);
15.});
16.}
17.};
18.this.each(function() {
19.textNodes(this);
20.});
21.return out;
22.};

» Source

“outerHTML” function

The well-known innerHTML property is very useful: It allows you to get the content of an HTML element. But what if you need the content, and also the HTML tags? You have to create an “outerHTML” function like this one:

1.jQuery.fn.outerHTML = function() {
2.return $('
3.<div>').append( this.eq(0).clone() ).html();
4.};</div>

» Source

Clean way to open popup windows

Although their popularity decreased with the rise of popup blockers, pop-up windows can still be useful in some particular cases. Here is a nice snippet to open links in pop-up windows. Just add the popupcss class to your link to make it work.

1.jQuery('a.popup').live('click', function(){
2.newwindow=window.open($(this).attr('href'),'','height=200,width=150');
3.if (window.focus) {newwindow.focus()}
4.return false;
5.});

» Source

Quick and easy browser detection

Cross-browser issues are definitely the biggest problem a front-end web developer has to face at work. Thanks to jQuery, detecting browsers have never been so easy, as shown below:

01.//A. Target Safari
02.if( $.browser.safari ) $("#menu li a").css("padding", "1em 1.2em" );
03.
04.//B. Target anything above IE6
05.if ($.browser.msie && $.browser.version > 6 ) $("#menu li a").css("padding","1em 1.8em" );
06.
07.//C. Target IE6 and below
08.if ($.browser.msie && $.browser.version <= 6 ) $("#menu li a").css("padding","1em 1.8em" );
09.
10.//D. Target Firefox 2 and above
11.if ($.browser.mozilla && $.browser.version >= "1.8" ) $("#menu li a").css("padding", "1em 1.8em" );

» Source

Get relative mouse position

Do you ever want to be able to get the relative mouse position? This very handy function will return the mouse position (x and y) according to its parent element.

1.function rPosition(elementID, mouseX, mouseY) {
2.var offset = $('#'+elementID).offset();
3.var x = mouseX - offset.left;
4.var y = mouseY - offset.top;
5.
6.return {'x': x, 'y': y};
7.}

» Source

Parse an xml file using jQuery

XML files are very important on the Internet, and any developer has to parse them from time to time. Thanks to jQuery and all its powerful functions, the whole process is painless, as demonstrated in the example code below:

1.function parseXml(xml) {
2.//find every Tutorial and print the author
3.$(xml).find("Tutorial").each(function()
4.{
5.$("#output").append($(this).attr("author") + "");
6.});
7.}

» Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s