Skip to content

Instantly share code, notes, and snippets.

@afahy
afahy / Random.js
Created October 10, 2009 22:03
Random #
(function($){
$.extend($, {
// expects n == positive; returns 0 to n exclusive
rand: function(n){
return Math.floor(n * Math.random());
}
})
})(jQuery);
@afahy
afahy / Subscribe.js
Created October 10, 2009 22:31
Subscribe
(function($){
$.extend($.fn, {
subscribe: function(el, evt, cb){
var self = this;
if(!self.length) { return self; }
el.bind(evt, function(){ cb.apply(self, arguments); });
return self;
}
});
})(jQuery);
@afahy
afahy / Overlay.js
Created October 11, 2009 03:31
Overlay
(function($){
$.overlay = (function(){
var f = function(params){
var opts = $.extend({}, f.defaults, params),
$window = $(window),
winHeight = $window.height(),
$elem = $("<div>").css(opts.css);
@afahy
afahy / zIndexed.js
Created October 15, 2009 23:48
Get elements with defined z-index
/* Was getzidx.js */
/* Now a custom selector, gives you more flexibility */
/* Use ala: $("*:z-indexed"); */
(function($){
$.extend(
$.expr[":"], { "z-indexed": function(e){ return $(e).css("zIndex") != "auto" } }
);
})(jQuery);
@afahy
afahy / watermark.js
Created October 22, 2009 17:47
Watermark fields
(function($){
$.fn.watermark = function(){
return this.each(function(){
var $this = $(this),
tag = this.nodeName.toLowerCase(),
txt;
if(tag == "input" || tag == "textarea"){
txt = $this.val();
$this.bind("focus.watermark", function(){
if($this.val() == txt){ $this.val(""); }
@afahy
afahy / template.js
Created October 22, 2009 19:07
Resig's JS templating
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
@afahy
afahy / opacity.js
Created October 22, 2009 19:26
Opacity shortcut
(function($){
// silly shortcut to get/set opacity
$.extend($.fn, {
opacity: function(n){
return n ? this.css("opacity",n) : this.css("opacity");
}
});
})(jQuery);
@afahy
afahy / maxlength.js
Created October 22, 2009 21:03
Maxlength on textarea
// Triggers "updated.maxlength" event when updated (post-keyup)
// evt sends: { maxlength: Number, remaining: Number }
(function($){
var f = function(num){
return this.each(function(){
var $this = $(this),
chars = num || $this.attr("data-maxlength") || f.num,
remaining = function(){ return chars - $this.val().length };
if(this.nodeName.toLowerCase() == "textarea"){
@afahy
afahy / isstring.js
Created November 12, 2009 21:04
Checks if arg is string
(function($){
// check if arg is a string
$.extend($, {
isString: function(s){
return (typeof s == "string" || s instanceof String)
}
});
})(jQuery);
@afahy
afahy / yql.js
Created November 17, 2009 20:04
Helper for YQL calls
// Simple YQL helper:
// $.yql({q: strQuery, callback: fnCallback });
(function($){
$.yql = function(o){
var params = $.extend({}, $.yql.options, o),
q = encodeURIComponent(o.query.toLowerCase()),
url = $.yql._url + "?q=" + q + "&format=json&callback=?";
$.getJSON(url, params.callback)