Skip to content

Instantly share code, notes, and snippets.

@partynight12th
Created August 29, 2012 11:06
Show Gist options
  • Save partynight12th/3510741 to your computer and use it in GitHub Desktop.
Save partynight12th/3510741 to your computer and use it in GitHub Desktop.
とりあえず動かしておきたいJS
var djs = {};//Default setting JavaScript
djs.w = window;
djs.d = djs.w.document;
//IEでconsole未定義エラーを出さないようにする。
if(!window.console){
(function(win){
var names = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ];
var consoleMock = {};
for(var i = 0, len = names.length; i < len; i++){
consoleMock[names[i]] = function(){};
}
win.console = consoleMock;
})(djs.w);
}
djs.b = function(browser){
djs.b.value = browser;
djs.b["is"+browser] = true;
djs.b.modern = !/^ie/i.test(browser);
}
if(typeof djs.d.documentElement.style.maxHeight !== "undefined"){
if(!/*@cc_on!@*/false){
if(typeof djs.w.mozAnimationStartTime !== "undefined"){
djs.b("firefox");
}else if(typeof djs.w.opera !== "undefined"){
djs.b("opera");
}else{
if(typeof djs.w.Image === "function"){
djs.b("chrome");
}else if(typeof djs.w.Image === "object"){
djs.b("safari");
}else{
djs.b("other");
}
}
}else if(typeof djs.d.documentMode !== "undefined"){
djs.b("ie"+djs.d.documentMode);
}else{
djs.b("ie7");
}
}else{
djs.b("ie6");
}
if(typeof jQuery !== "undefined"){
console.info("jQuery version : " + jQuery().jquery);
djs.parentid = function(t){
var t$ = jQuery(t);
var pid = t$.attr("id");
if(pid && pid.length > 0){
}else if(t$.get(0).tagName.match(/body/i)){
pid = "_body";
}else{
pid = djs.parentid(t$.parent());
}
return pid;
}
djs.unloader = function(){
jQuery(djs.d).off("click", "a, area");
}
djs.anchortrace = function(){
jQuery(djs.d).on("click", "a, area", function(){
var href = jQuery(this).attr("href");
var pathname = location.pathname;
if(href && !href.match("^http")){
href = pathname.replace(/[^\/]*$/,"") + href;
while(href.match(/\.\.\//)){
href = href.replace(/[^\/]*\/\.\.\//, "");
}
href = href.replace(/\.\//,"").replace(/\/\//, "/");
}
pathname += location.search;
//console.debug("_trackEvent", pathname, "#" + djs.parentid(this), href);
_gaq.push(["_trackEvent", pathname, "#" + djs.parentid(this), href ]);
});
jQuery(djs.w).unload(this.unloader);
}
jQuery(function(){
if("_gaq" in djs.w){
djs.anchortrace();
}
});
djs.datevalue = {
weekname:["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
monthname:["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};
djs.cookie = {
get: function(key){
var cookies = djs.d.cookie;
var cookiesarray = cookies.split(";");
var len = cookiesarray.length;
var reg = new RegExp("^\\s*" + key + "=");
for(var i = 0; i < len; i++){
if(reg.test(cookiesarray[i])){
return unescape(cookiesarray[i].split("=")[1]);
}
}
},
set: function(key, value, expires){
var cookieRecipe = key + "=" + escape(value) + ";";
if(expires||false){
//Expires format: Mon, 31-Jan-2011 12:34:56 GMT
var normalexp = new Date(expires);
if(!/Invalid Date/.test(normalexp.toString)){
var normalexpstring = djs.datevalue.weekname[normalexp.getUTCDay()] + ", " +
normalexp.getUTCDate() + "-" +
djs.datevalue.monthname[normalexp.getUTCMonth()] + "-" +
normalexp.getUTCFullYear() + " " +
normalexp.getUTCHours() + ":" +
normalexp.getUTCMinutes() + ":" +
normalexp.getUTCSeconds() + " " +
"GMT";
cookieRecipe += " " + "expires=" + normalexpstring + ";";
}
}
djs.d.cookie = cookieRecipe;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment