javascript-magic
Version:
Include Ray's JS Library
137 lines (125 loc) • 3.37 kB
JavaScript
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
/* console
if (typeof console == "undefined")
var console = {
log: function (msg) {
consoleMsg (msg);
}
};
var consoleMsg = function (msg) {
//do something with msg
};
end console */
window.debug = {
message: function (m) {
if (window.shouldDebug) {
console.log (m);
}
},
warn: function (m) {
if (window.shouldDebug) {
console.warn (m);
}
},
error: function (m) {
if (window.shouldDebug) {
console.error (m);
}
}
}
window.breakpoints = [];
window.breakpoints ['x-small'] = 320;
window.breakpoints ['smaller'] = 360;
window.breakpoints ['small'] = 495;
window.breakpoints ['medium'] = 671;
window.breakpoints ['medium-large'] = 768;
window.breakpoints ['large'] = 847;
window.breakpoints ['x-large'] = 1023;
window.breakpoints ['max'] = 1024;
window.debounce = function (func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply (context, args);
};
var callNow = immediate && !timeout;
clearTimeout (timeout);
timeout = setTimeout (later, wait);
if (callNow) func.apply (context, args);
};
};
/*cookie stuff*/
window.createCookie = function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
};
window.readCookie = function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
};
window.deleteCookie = function (name) {
window.createCookie (name, "", -2);
};
/*end cookie stuff*/
/*request params*/
args = document.location.search.substring (1).split ('&');
window.requestVariables = {};
for (i=0; i < args.length; i++) {
arg = args [i];
if (arg.indexOf ('=') == -1)
window.requestVariables [arg.trim ()] = true;
else {
kvp = arg.split ('=');
window.requestVariables [kvp [0].trim ()] = kvp [1].trim ();
}
}
/*end request params*/
window.transitionEvent = function () {
var t;
var el = document.createElement ('fakeelement');
var transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
}
for (t in transitions) {
if (el.style[t] !== undefined) {
return transitions [t];
}
}
};
window.animationEvent = function () {
var t;
var el = document.createElement ('fakeelement');
var animations = {
'animation':'animationend',
'OAnimation':'oAnimationEnd',
'MozAnimation':'animationend',
'WebkitAnimation':'webkitAnimationEnd'
}
for (t in animations) {
if (el.style[t] !== undefined) {
return animations [t];
}
}
};
window.transitionEnd = window.transitionEvent ();
window.animationEnd = window.animationEvent ();