Wednesday 5 December 2012

JQuery hacks, tips and tricks.

1. Target blank links

Do you use the target=blank attribute on links? If yes, you might know that XHTML 1.0 Strict don’t allow it. A good solution to this problem should be using JQuery to make links opening in new windows:
 
 $('a[@rel$='external']').click(function(){

 this.target = "_blank";

 });

/*
Usage:
<a href="http://www.lepinskidesign.com.br/" rel="external">lepinskidesign.com.br</a>
*/

2. Get the total number of matched elements

That what I call a very simple, but very useful tip: This will return the number of matched elements:

 $('element').size(); 

3. Preloading images

When you’re using images in Javascript, a good thing is to preload it before you have to use it. This code will do the job:

jQuery.preloadImages = function(){
      for(var i = 0; i").attr("src", arguments[i]);
}
};
// Usage
$.preloadImages("image1.gif", "/path/to/image2.png", "some/image3.jpg");


4. Detect browser

Although it is better to use CSS conditionnal comments to detect a specific browser and apply some css style, it is a very easy thing to do with JQuery, which can be useful at times.

 //A. Target Safari

 if( $.browser.safari ) $("#menu li a").css("padding", "1em 1.2em" );

 

 //B. Target anything above IE6

 if ($.browser.msie && $.browser.version > 6 )$("#menu li a").css("padding", "1em 1.8em" );

 

 //C. Target IE6 and below

 if ($.browser.msie && $.browser.version <= 6 ) $("#menu li a").css("padding", "1em 1.8em" );

 

 //D. Target Firefox 2 and above

 if ($.browser.mozilla && $.browser.version >= "1.8" ) $("#menu li a").css("padding", "1em 1.8em"

 );

 


5. Remove a word

in a text Do you ever wanted to be able to remove words in a text? Note that the following code can be easily modified to replace a word by another.

 var el = $('#id');

 el.html(el.html().replace(/word/ig, ""));

 
6. Columns of equal height

This seems to be a highly-requested hack: How to use two CSS columns, and make them having exactly the same height? Happilly Rob from cssnewbie have the solution.

 function equalHeight(group) {

 tallest = 0;

 group.each(function() {

 thisHeight = $(this).height();

 if(thisHeight > tallest) {

 tallest = thisHeight;

 }

 });

 group.height(tallest);

 }

 

 /* Usage:

 $(document).ready(function() {

 equalHeight($(".recent-article"));

 equalHeight($(".footer-col"));

 });

 */

 
Source: Equal Height Columns with jQuery

7. Font resizing

Font Resizing is a very common feature in many modern websites. Here’s how to do it with JQuery.

 $(document).ready(function(){

 // Reset Font Size

 var originalFontSize =

 $('html').css('font-size');

 $(".resetFont").click(function(){ $('html').css('font-size', originalFontSize);

 });

 // Increase Font Size

 $(".increaseFont").click(function(){

 var currentFontSize = $('html').css('font-size');

 var currentFontSizeNum = parseFloat(currentFontSize, 10);

 var newFontSize = currentFontSizeNum*1.2;

 $('html').css('font-size', newFontSize);

 return false;

 });

 });

 
// Decrease Font Size $(".decreaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*0.8; $('html').css('font-size', newFontSize); return false; }); }); [/cc] Source: Text Resizing With jQuery
8. Disable right-click contextual menu

There’s many Javascript snippets available to disable right-click contextual menu, but JQuery makes things a lot easier:

 $(document).ready(function(){

 $(document).bind("contextmenu",function(e){

 return false;

 });

 });

No comments:

Post a Comment

Do you think it could be useful for you? Share your thoughts with us!