Skip to content

Instantly share code, notes, and snippets.

@beiyuu
Created March 13, 2012 16:53
Show Gist options
  • Save beiyuu/2029896 to your computer and use it in GitHub Desktop.
Save beiyuu/2029896 to your computer and use it in GitHub Desktop.
Javascript:Tools
//********** CLASS **********
hasClass = function (el, cl) {
var regex = new RegExp('(?:\\s|^)' + cl + '(?:\\s|$)');
return !!el.className.match(regex);
},
addClass = function (el, cl) {
el.className += ' ' + cl;
},
removeClass = function (el, cl) {
var regex = new RegExp('(?:\\s|^)' + cl + '(?:\\s|$)');
el.className = el.className.replace(regex, ' ');
},
toggleClass = function (el, cl) {
hasClass(el, cl) ? removeClass(el, cl) : addClass(el, cl);
};
//********** FORMAT TIME **********
var formatTime = function(seconds) {
var printDate = false;
var timeFormat = '';
var myHtml = '';
var Stamp = new Date();
switch(timeFormat) {
case 'locale' : Stamp.toLocaleString(); break;
case 'gtmdiff' : Stamp.getTimezoneOffset(); break;
case 'gtm' : Stamp.toGMTString(); break;
}
// DATE
if (printDate) {
var y = Stamp.getFullYear();
var m = (Stamp.getMonth() + 1).toPaddedString(2);
var d = Stamp.getDate();
myHtml += d+'/'+m+'/'+y;
}
// TEMPS
var he = Stamp.getHours().toPaddedString(2);
var mi = Stamp.getMinutes().toPaddedString(2);
var se = Stamp.getSeconds().toPaddedString(2);
myHtml += he+'h'+mi+'min'+se+'sec';
return myHtml;
};
//********** INCLUDE SCRIPT **********
BYB.includeScript = function(file,callback){
var _doc = document.getElementsByTagName('head')[0];
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', file);
_doc.appendChild(js);
if (!/*@cc_on!@*/0) { //if not IE
//Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload
js.onload = function () {
callback();
}
} else {
//IE6、IE7 support js.onreadystatechange
js.onreadystatechange = function () {
if (js.readyState == 'loaded' || js.readyState == 'complete') {
callback();
}
}
}
return false;
}
//********** CHECK IMAGE COMPLETED **********
if(BYB.img.complete){
imgComplete()
}else{
BYB.img.onload = imgComplete;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment