RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

27 July 10

[JS] jQuery random number plugin

I’ve taken this little piece of code around the Web (sorry, I forgot the original source) and it’s a good example on how to develop a plugin for jQuery:

/** Usage:
* $.random(int);
* $.randomBetween(min, max); */
jQuery.extend({
	random: function(X) {
		return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

Comments (View)
10 September 09

[jQuery] How to use Prototype or MooTools with jQuery

Since many libraries make use of the $ identifier, we need a way to prevent collisions between these names. jQuery provides a method called .noConflict() to return control of the $ identifier back to other libraries. Typical usage of .noConflict() follows the following pattern:

First, the other library (Prototype in this example) is included. Then, jQuery itself is included, taking over $ for its own use. Next, a call to .noConflict() frees up $, so that control of it reverts to the first included library (Prototype). Now in our custom script we can use both libraries—but whenever we need to use a jQuery method, we need to use jQuery instead of $ as an identifier.

Comments (View)
Posted: 12:11 PM

[jQuery] How to add a CSS class to external links

 // apply a class to all <a> which respect the filter selection.
  $('a').filter(function() {
    return this.hostname && this.hostname != location.hostname;
  }).addClass('external');

Comments (View)
Themed by Hunson. Originally by Josh