Skip to content

Instantly share code, notes, and snippets.

@code code/bagoftricks.js

Created May 24, 2010
Embed
What would you like to do?
var noop = function(){};
var console = window['console'] || {log: noop, debug: noop, info: noop, warn: noop, error: function(args){alert(args);}};
var loadScriptTag = function(src, loaded, callback) {
if(loaded()){
callback();
return;
}
console.log("Loading missing javascript.", "swfobject", src);
var scriptTag=document.createElement('scr'+'ipt');
scriptTag.type = 'text/javascript';
scriptTag.src=src;
var head = document.getElementsByTagName("head")[0];
head.appendChild(scriptTag);
function check(){
if(loaded()){
callback();
} else {
setTimeout(check, 50);
}
};
check();
};
var hasClassName = function(element, className) {
var elementClassName = element.className;
return (elementClassName.length > 0 && (elementClassName == className ||
new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
};
var addClassName = function(element, className) {
if (!hasClassName(element, className))
element.className += (element.className ? ' ' : '') + className;
return element;
};
var removeClassName = function(element, className) {
element.className = element.className.replace(
new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
return element;
};
var lower = function(obj){
return obj.toString().toLowerCase();
};
var collect = function(list,func){
var x = [];
for(var i=0; i<list.length; i++){
if(func(list[i])){
x.push(list[i]);
}
}
return x;
}
var each = function(list,func){
var x = [];
for(var i=0; i<list.length; i++){
x.push(func(list[i]));
}
return x;
}
var first = function(list,func){
var x;
for(var i=0; i<list.length; i++){
x = func(list[i]);
if(x){
return list[i];
}
}
}
var merge = function(a,b){
obj = {};
for(prop in a){
obj[prop] = a[prop];
}
for(prop in b){
obj[prop] = b[prop];
}
return obj;
}
var deferUntilReady = function(callback){
var readyRun = false;
var ready = function(){
if(readyRun){
return;
}
readyRun = true;
callback();
}
var domReady = null;
if ( document.addEventListener ) {
domReady = function() {
document.removeEventListener( "DOMContentLoaded", domReady, false );
ready();
};
} else if ( document.attachEvent ) {
domReady = function() {
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", domReady );
ready();
}
};
}
var bindRun = false;
var bind = function() {
if(bindRun) {
return;
}
bindRun = true;
if ( document.readyState === "complete" ) {
return ready();
}
if ( document.addEventListener ) {
document.addEventListener( "DOMContentLoaded", domReady, false );
window.addEventListener( "load", ready, false );
} else if ( document.attachEvent ) {
document.attachEvent("onreadystatechange", domReady);
window.attachEvent( "onload", ready );
}
};
bind();
}
var dispatchEvent = function(obj, type, bubble, canCancel){
if(bubble == undefined){
bubble = true;
}
if(canCancel == undefined){
canCancel = true;
}
if(document.createEvent){
var e = document.createEvent('HTMLEvents');
if(e.initEvent){
e.initEvent(type, bubble, canCancel);
}
if(obj.dispatchEvent){
obj.dispatchEvent(e);
}
} else if(obj.fireEvent){
obj.fireEvent('on'+type);
}
};
var setData = function(obj, data){
for(var prop in data){
obj.setAttribute('data-'+prop, data[prop]);
}
};
var getData = function(obj){
var data = {};
for(var i=0; i<obj.attributes.length; i++){
if(obj.attributes[i].nodeName.indexOf('data-')==0){
data[obj.attributes[i].nodeName.split("data-")[1]] = obj.attributes[i].nodeValue;
}
}
return data;
};
var randId = function(){
return "rand_"+Math.random().toString().replace('.','');
};
var validElementId = function(element){
if(element.id){
return element.id;
} else {
element.id = randId();
return element.id;
}
};
if(!document.getElementsByClassName){
document.getElementsByClassName = function(className, parentElement) {
if (Prototype.BrowserFeatures.XPath) {
var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
return document._getElementsByXPath(q, parentElement);
} else {
var children = ($(parentElement) || document.body).getElementsByTagName('*');
var elements = [], child;
for (var i = 0, length = children.length; i < length; i++) {
child = children[i];
if (Element.hasClassName(child, className))
elements.push(Element.extend(child));
}
return elements;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.