Created
July 16, 2012 20:44
-
-
Save ericwwsun/3124925 to your computer and use it in GitHub Desktop.
Javascript: Project Js Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $j = jQuery.noConflict(); | |
$j(document).ready(function(){ | |
ms.header.init(); | |
ms.home.init(); | |
ms.footer.init(); | |
}); // end of document ready | |
/* ------------------------------------------ | |
* Project Namespace | |
* ------------------------------------------ */ | |
var ms = ms || {}; | |
ms.Utils = ms.Utils || {}; | |
/* ------------------------------------------ | |
* User Interface Control | |
* ------------------------------------------ */ | |
ms.header = (function(){ | |
var init = function(){ | |
console.log('header init'); | |
}; | |
return { | |
init: init | |
} | |
}()); | |
ms.footer = (function(){ | |
var init = function(){ | |
console.log('footer init'); | |
}; | |
return { | |
init: init | |
} | |
}()); | |
ms.home = (function(){ | |
var init = function(){ | |
console.log('home init'); | |
}; | |
return { | |
init: init | |
}; | |
}()); | |
/* ------------------------------------------ | |
* Utilities & Modules | |
* ------------------------------------------ */ | |
ms.Utils = { | |
isMobile: function() { | |
// detect user agent | |
var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase())); | |
var result = false; | |
if (mobile) { | |
result = true; | |
var userAgent = navigator.userAgent.toLowerCase(); | |
if ((userAgent.search("android") > -1) && (userAgent.search("mobile") > -1)) | |
result = true; // android mobile | |
else if ((userAgent.search("android") > -1) && !(userAgent.search("mobile") > -1)) | |
result = false; // android tablet | |
} | |
return result; | |
}, | |
validateEmail : function(email){ | |
var filter = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/ | |
if(filter.test(email)){ | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
setCookie: 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=/"; | |
}, | |
getCookie: 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; | |
}, | |
deleteCookie: function(name) { | |
setCookie(name,"",-1); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment