本人自用的global.js库源码分享


Posted in Javascript onFebruary 28, 2015
var GLOBAL = {};
GLOBAL.namespace = function(str) {
  var arr = str.split("."), o = GLOBAL,i;
  for (i = (arr[0] = "GLOBAL") ? 1 : 0; i < arr.length; i++) {
    o[arr[i]] = o[arr[i]] || {};
    o = o[arr[i]];
  }
};
//Dom相关
GLOBAL.namespace("Dom");

GLOBAL.Dom.getNextNode = function (node) {
  node = typeof node == "string" ? document.getElementById(node) : node;
  var nextNode = node.nextSibling;
  if (!nextNode) {
    return null;
  }
  if (!document.all) {
    while (true) {
      if (nextNode.nodeType == 1) {
        break;

      } else {
        if (nextNode.nextSibling) {
          nextNode = nextNode.nextSibling;
        } else {
          break;
        }
      }
    }
    return nextNode;
  }
}

GLOBAL.Dom.setOpacity = function(node, level) {
  node = typeof node == "string" ? document.getElementById(node) : node;
  if (document.all) {
    node.style.filter = 'alpha(opacity=' + level + ')';
  } else {
    node.style.opacity = level / 100;
  }
};

GLOBAL.Dom.getElementsByClassName = function (str, root, tag) {
  if (root) {
    root = typeof root == "string" ? document.getElementById(root) : root;
  } else {
    root = document.body;
  }
  tag = tag || "*";
  var els = root.getElementsByTagName(tag), arr = [];
  for (var i = 0, n = els.length; i < n; i++) {
    for (var j = 0, k = els[i].className.split(" "), l = k.length; j < l; j++) {
      if (k[j] == str) {
        arr.push(els[i]);
        break;
      }
    }
  }
  return arr;
}
GLOBAL.namespace("Event");
GLOBAL.Event.stopPropagation = function(e) {
  e = window.event || e;
  if (document.all) {
    e.cancelBubble = true;
  } else {
    e.stopPropagation();
  }
};
GLOBAL.Event.getEventTarget = function(e) {
  e = window.event || e;
  return e.srcElement || e.target;
};

GLOBAL.Event.on = function(node, eventType, handler) {
  node = typeof node == "string" ? document.getElementById(node) : node;
  if (document.all) {
    node.attachEvent("on" + eventType, handler);
  } else {
    node.addEventListener(eventType, handler, false);
  }
};

//Lang相关
GLOBAL.namespace("Lang");
GLOBAL.Lang.trim = function(ostr) {
  return ostr.replace(/^\s+|\s+$/g, "");
};

GLOBAL.Lang.isNumber = function(s) {
  return !isNaN(s);
};

function isString(s) {
  return typeof s === "string";
}



function isBoolean(s) {
  return typeof s === "boolean";
}

function isFunction(s) {
  return typeof s === "function";
}

function isNull(s) {
  return s === null;
}

function isUndefined(s) {
  return typeof s === "undefined";
}

function isEmpty(s) {
  return /^\s*$/.test(s);
}

function isArray(s) {
  return s instanceof Array;
}

GLOBAL.Dom.get = function (node) {
  node = typeof node === "string" ? document.getElementById(node) : node;
  return node;
}

function $(node) {
  node = typeof node == "string" ? document.getElementById(node) : node;
  return node;
}


GLOBAL.Lang.extend = function(subClass, superClass) {
  var F = function() {
  };
  F.prototype = superClass.prototype;
  subClass.prototype = new F();
  subClass.prototype.constructor = subClass;
  subClass.superClass = subClass.prototype;
  if (superClass.prototype.constructor == Object.prototype.constructor) {
    superClass.prototype.constructor = superClass;
  }
};

GLOBAL.namespace("Cookie");
GLOBAL.Cookie = {
  read: function (name) {
    var cookieStr = ";" + document.cookie + ";";
    var index = cookieStr.indexOf(";" + name + "=");
    if (index != -1) {
      var s = cookieStr.substring(index + name.length + 3, cookieStr.length);
      return unescape(s.substring(0, s.indexOf(";")));
    } else {
      return null;
    }
  },
  set: function (name, value, expires) {
    var expDays = expires * 24 * 60 * 60 * 1000;
    var expDate = new Date();
    expDate.setTime(expDate.getTime() + expDays);
    var expString = expires ? ";expires=" + expDate.toGMTString() : "";
    var pathString = ";path=/";
    document.cookie = name + "=" + escape(value) + expString + pathString;
  },
  del: function (name, value, expires) {
    var exp = new Date(new Date().getTime() - 1);
    var s = this.read(name);
    if (s != null) {
      document.cookie = name + "=" + s + ";expires=" + exp.toGMTString() + ";path=/";
    }
  }
};
Javascript 相关文章推荐
基于Jquery的文字滚动跑马灯插件(一个页面多个滚动区)
Jul 26 Javascript
url地址自动加#号问题说明
Aug 21 Javascript
同时使用n个window onload加载实例介绍
Apr 25 Javascript
javascript操作referer详细解析
Mar 10 Javascript
jquery实现导航固定顶部的效果仿蘑菇街
Oct 22 Javascript
jquery简单倒计时实现方法
Dec 18 Javascript
Vue2学习笔记之请求数据交互vue-resource
Feb 23 Javascript
js判断PC端与移动端跳转
Dec 24 Javascript
jQuery实现的鼠标滚轮控制图片缩放功能实例
Oct 14 jQuery
JavaScript数据结构之双向链表定义与使用方法示例
Oct 27 Javascript
Vue弹出菜单功能的实现代码
Sep 12 Javascript
Vue CLI2升级至Vue CLI3的方法步骤
May 20 Javascript
JS限制文本框只能输入数字和字母方法
Feb 28 #Javascript
javascript计时器详解
Feb 28 #Javascript
Lab.js初次使用笔记
Feb 28 #Javascript
js实现鼠标感应图片展示的方法
Feb 27 #Javascript
JQuery基础语法小结
Feb 27 #Javascript
JS实现网页背景颜色与select框中颜色同时变化的方法
Feb 27 #Javascript
分析了一下JQuery中的extend方法实现原理
Feb 27 #Javascript
You might like
PHP如何通过表单直接提交大文件详解
2019/01/08 PHP
PHP token验证生成原理实例分析
2019/06/05 PHP
在js(jquery)中获得文本框焦点和失去焦点的方法
2012/12/04 Javascript
js网页实时倒计时精确到秒级
2014/02/10 Javascript
javascript匿名函数实例分析
2014/11/18 Javascript
JavaScript中的getTimezoneOffset()方法使用详解
2015/06/10 Javascript
jquery验证邮箱格式是否正确实例讲解
2015/11/16 Javascript
JS实现pasteHTML兼容ie,firefox,chrome的方法
2016/06/22 Javascript
jQuery修改DOM结构_动力节点Java学院整理
2017/07/05 jQuery
简单理解Vue中的nextTick方法
2018/01/30 Javascript
Taro集成Redux快速上手的方法示例
2018/06/21 Javascript
浅析JavaScript异步代码优化
2019/03/18 Javascript
vue登录注册实例详解
2019/09/14 Javascript
vue路由拦截器和请求拦截器知识点总结
2019/11/08 Javascript
[39:08]完美世界DOTA2联赛PWL S3 LBZS vs CPG 第一场 12.12
2020/12/16 DOTA
使用Python下载歌词并嵌入歌曲文件中的实现代码
2015/11/13 Python
Python中使用urllib2模块编写爬虫的简单上手示例
2016/01/20 Python
python的文件操作方法汇总
2017/11/10 Python
python requests爬取高德地图数据的实例
2018/11/10 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
Django  ORM 练习题及答案
2019/07/19 Python
python调用接口的4种方式代码实例
2019/11/19 Python
python使用openpyxl操作excel的方法步骤
2020/05/28 Python
Django中template for如何使用方法
2021/01/31 Python
详解基于 Canvas 手撸一个六边形能力图
2019/09/02 HTML / CSS
html5的自定义data-*属性与jquery的data()方法的使用
2014/07/02 HTML / CSS
女士鞋子、包包和服装在线,第一款10美元:ShoeDazzle
2019/07/26 全球购物
英国旅行箱包和行李箱购物网站:Travel Luggage & Cabin Bags
2019/08/26 全球购物
巴西备受欢迎的服装和生活方式品牌:FARM Rio
2020/02/04 全球购物
酒店管理专业毕业生求职自荐信
2014/04/28 职场文书
员工团队活动方案
2014/08/28 职场文书
竞选学习委员演讲稿
2014/09/01 职场文书
生活委员竞选稿
2015/11/21 职场文书
关于Javascript闭包与应用的详解
2021/04/22 Javascript
Python中基础数据类型 set集合知识点总结
2021/08/02 Python
「SHOW BY ROCK!!」“雫シークレットマインド”组合单曲MV公开
2022/03/21 日漫