Skip to content

Instantly share code, notes, and snippets.

View samuelcole's full-sized avatar

Sam Cole samuelcole

View GitHub Profile
@samuelcole
samuelcole / jquery.validate.js
Created January 4, 2011 21:03
Determines validity by running through an array of functions in the data object.
// Checks if a DOM object is valid.
// Expects an array of functions in data('validations') that return true when
// valid.
//
// Triggers field_invalid or field_valid.
// Calls options.invalid() or options.valid().
$.fn.validate = function(options) {
var $this = $(this);
/* $this.data('validations') is an array of functions, each of which
@samuelcole
samuelcole / match_protocol.js
Created December 13, 2010 23:51
A little function that accepts a url, and returns a url that matches the current protocol.
window.match_protocol = function(url) {
var RE_PROTO = /^\w+:\/\//;
if(RE_PROTO.test(url)) {
var protocol = window.location.protocol;
url = url.replace(RE_PROTO, protocol + "//");
}
return url;
};
@samuelcole
samuelcole / bootstrap_getScript.js
Created April 13, 2010 15:50
zero dependency $.getScript for loading large external javascripts
// zero dependency $.getScript for loading external javascript (like jQuery.js
// or other big libraries) in a way that does not block the normal page load
// (http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/).
//
// heavily influenced by/copied from jQuery's ajax functions (http://jquery.com)
window.$ = {
getScript: function(script_src, callback) {
var done = false;
var head = document.getElementsByTagName("head")[0] || document.documentElement;
@samuelcole
samuelcole / jquery.requires.js
Created March 9, 2010 18:48
$.requires function for loading external javascript requirements
window.requires_urls = {};
(function($){
$.requires = function(url, callback) {
// look for the url in our cached url results
for(called_url in window.requires_urls) {
var cached = window.requires_urls[called_url].cached;
// if a url matches and it has cached results
if(url_equals(url, called_url) && cached.data) {