本人自用的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 相关文章推荐
js关闭浏览器窗口及检查浏览器关闭事件
Sep 03 Javascript
checkbox选中与未选中判断示例
Aug 04 Javascript
jQuery下拉美化搜索表单效果代码分享
Aug 25 Javascript
轻松掌握JavaScript状态模式
Sep 07 Javascript
AngularJS中如何使用echart插件示例详解
Oct 26 Javascript
Angular项目中$scope.$apply()方法的使用详解
Jul 26 Javascript
Kindeditor单独调用单图上传增加预览功能的实例
Jul 31 Javascript
ComboBox(下拉列表框)通过url加载调用远程数据的方法
Aug 06 Javascript
javascript创建元素和删除元素实例小结
Jun 19 Javascript
js 实现ajax发送步骤过程详解
Jul 25 Javascript
JavaScript实现联动菜单特效
Jan 07 Javascript
vue大型项目之分模块运行/打包的实现
Sep 21 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
咖啡知识大全
2021/03/03 新手入门
php下的权限算法的实现
2007/04/28 PHP
10个可以简化php开发过程的MySQL工具
2010/04/11 PHP
php中var_export与var_dump的区别分析
2010/08/21 PHP
php获取文件夹路径内的图片以及分页显示示例
2014/03/11 PHP
一文掌握PHP Xdebug 本地与远程调试(小结)
2019/04/23 PHP
PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】
2019/04/27 PHP
php反射学习之依赖注入示例
2019/06/14 PHP
php依赖注入知识点详解
2019/09/23 PHP
javascript随机将第一个dom中的图片添加到第二个div中示例
2013/10/08 Javascript
js关于字符长度限制的问题示例探讨
2014/01/24 Javascript
对new functionName()定义一个函数的理解
2014/05/22 Javascript
JavaScript定时器setTimeout()和setInterval()详解
2017/08/18 Javascript
JS实现的简单分页功能示例
2018/08/23 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
2018/08/25 Javascript
js中的面向对象之对象常见创建方法详解
2019/12/16 Javascript
Javascript实现html转pdf高清版(提高分辨率)
2020/02/19 Javascript
python登录并爬取淘宝信息代码示例
2017/12/09 Python
配置 Pycharm 默认 Test runner 的图文教程
2018/11/30 Python
python实现五子棋人机对战游戏
2020/03/25 Python
使用pyinstaller打包PyQt4程序遇到的问题及解决方法
2019/06/24 Python
在OpenCV里使用Camshift算法的实现
2019/11/22 Python
Python多线程threading join和守护线程setDeamon原理详解
2020/03/18 Python
无需压缩软件,用python帮你操作压缩包
2020/08/17 Python
互斥锁解决 Python 中多线程共享全局变量的问题(推荐)
2020/09/28 Python
台湾租车首选品牌:IWS艾维士租车
2019/05/03 全球购物
注塑工厂厂长岗位职责
2013/12/02 职场文书
培训主管的职业生涯规划
2014/03/06 职场文书
财务总监岗位职责
2014/03/07 职场文书
解除劳动合同协议书范本
2014/09/13 职场文书
2014年初一班主任工作总结
2014/11/08 职场文书
小学新教师个人总结
2015/02/05 职场文书
中班下学期个人总结
2015/02/12 职场文书
SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 WIN-8IVSNAQS8T7\Administrator 的信息
2021/06/30 SQL Server
解决mysql的int型主键自增问题
2021/07/15 MySQL
Python的property属性详细讲解
2022/04/11 Python