本人自用的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 相关文章推荐
子窗口、父窗口和Silverlight之间的相互调用
Aug 16 Javascript
js实现一个省市区三级联动选择框代码分享
Mar 06 Javascript
JS中的form.submit()不能提交表单的错误原因
Oct 08 Javascript
javascript数据类型示例分享
Jan 19 Javascript
javascript定时器完整实例
Feb 10 Javascript
javascript创建对象的几种模式介绍
May 06 Javascript
JS常用算法实现代码
Nov 14 Javascript
js Canvas绘制圆形时钟教程
Feb 06 Javascript
JavaScript运动框架 解决速度正负取整问题(一)
May 17 Javascript
JavaScript继承与多继承实例分析
May 26 Javascript
jQuery实现条件搜索查询、实时取值及升降序排序的方法分析
May 04 jQuery
js 压缩图片的示例(只缩小体积,不更改图片尺寸)
Oct 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
PHP中MD5函数使用实例代码
2008/06/07 PHP
PHP的Yii框架入门使用教程
2016/02/15 PHP
PHP实现通过URL提取根域名
2016/03/31 PHP
Thinkphp5 自定义上传文件名的实现方法
2019/07/23 PHP
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
js添加table的行和列 具体实现方法
2013/07/22 Javascript
js实现背景图片感应鼠标变化的方法
2015/02/28 Javascript
浅谈javascript中的三种弹窗
2016/10/21 Javascript
JavaScript两个变量交换值的实现方法
2017/03/01 Javascript
webpack 2的react开发配置实例代码
2017/07/28 Javascript
移动web开发之touch事件实例详解
2018/01/17 Javascript
jQuery中可见性过滤器简单用法示例
2018/03/31 jQuery
JavaScript实现简单的隐藏式侧边栏功能示例
2018/08/31 Javascript
js canvas实现5张图片合成一张图片
2019/07/15 Javascript
Vue可自定义tab组件用法实例
2019/10/24 Javascript
vue中watch的用法汇总
2020/12/28 Vue.js
微信跳一跳python辅助脚本(总结)
2018/01/11 Python
python+pandas生成指定日期和重采样的方法
2018/04/11 Python
Python实现读取字符串按列分配后按行输出示例
2018/04/17 Python
如何使用django的MTV开发模式返回一个网页
2019/07/22 Python
Python装饰器的应用场景代码总结
2020/04/10 Python
浅谈Python中的生成器和迭代器
2020/06/19 Python
Django静态文件加载失败解决方案
2020/08/26 Python
Python中猜拳游戏与猜筛子游戏的实现方法
2020/09/04 Python
Python实现小黑屋游戏的完整实例
2021/01/06 Python
基本款天堂:Everlane
2017/05/13 全球购物
哥德堡通行证:Gothenburg Pass
2019/12/09 全球购物
新年爱情寄语
2014/04/08 职场文书
活动总结怎么写
2014/04/28 职场文书
文明礼仪伴我行演讲稿
2014/05/12 职场文书
学校督导评估方案
2014/06/10 职场文书
道德与公民自我评价
2015/03/09 职场文书
《敬重卑微》读后感3篇
2019/11/26 职场文书
pygame面向对象的飞行小鸟实现(Flappy bird)
2021/04/01 Python
Redis RDB技术底层原理详解
2021/09/04 Redis
python读取mat文件生成h5文件的实现
2022/07/15 Python