如下所示:

// 重复字符串
String.prototype.repeat = function(n) {
  return new Array(n+1).join(this);
}
 
// 替换全部
String.prototype.replaceAll = function(str1, str2) {
  return this.replace(new RegExp(str1, "gm"), str2);
}
 
// 清除空格
String.prototype.trim = function() {
  return this.replace(/^\s*(.*"$1");
}
 
// 计算数组中的最大值
Array.prototype.max = function() {
  return Math.max.apply({}, this);
}
 
// 计算数组中的最小值
Array.prototype.min = function() {
  return Math.min.apply({}, this);
}
 
// 复制数组
Array.prototype.copy = function() {
  return [].concat(this);
};
 
// 去除数组中指定元素,只能去除一个,如果想多个,之前先用unique处理
Array.prototype.remove = function(value){
  for (var i = 0, len = this.length; i < len; i++) {
    if (this[i] == value) {
      this.splice(i, 1);
      break;
    }
  }
  return this;
}
 
// 判断数组中是否存在指定元素,返回索引值
Array.prototype.inArray = function(value) {
  var index = -1, key;
  for (key in this) {
    if (this[key] == value) {
      index = key;
      break;
    }
  }
  return index;
}
 
// 去除数组中的重复元素
Array.prototype.unique = function() {
  var key, ret = [];
  for (key in this) {
    if (ret.inArray(this[key]) < 0) {
      ret.push(this[key]);
    }
  }
  return ret;
}
 
// 检测是否已经安装flash,检测flash的版本
var flashVersion = (function() {
  var version;
  try {
    version = navigator.plugins['Shockwave Flash'];
    version = version.description;
  } catch (ex) {
    try {
      version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
        .GetVariable('$version');
    } catch (ex2) {
      version = '0.0';
    }
  }
  version = version.match(/\d+/g);
  return parseFloat(version[0] + '.' + version[1], 10);
})();
 
// 检测是否支持transition
var supportTransition = (function() {
  var s = document.createElement('p').style,
    r = 'transition' in s ||
    'WebkitTransition' in s ||
    'MozTransition' in s ||
    'msTransition' in s ||
    'OTransition' in s;
  s = null;
  return r;
})();
 
// 判断浏览器是否支持图片的base64
var isSupportBase64 = (function() {
  var data = new Image();
  var support = true;
  data.onload = data.onerror = function() {
    if (this.width != 1 || this.height != 1) {
      support = false;
    }
    return support;
  };
  data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
})();
 
// 首字母大写
function ucfirst(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}
 
// 清除左空格
function ltrim(str) {
  return str.replace(/^(\s*| *)/, "");
}
 
// 清除右空格
function rtrim(str) {
  return str.replace(/(\s*| *)$/, "");
}
 
// 设置Cookie值
function setCookie(name, value, hours, path, domain) {
  var d = new Date();
  var offset = 8;
  var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
  var nd = utc + (3600000 * offset);
  var expire = new Date(nd);
  expire.setTime(expire.getTime() + hours * 60 * 60 * 1000);
  var path = path || "";
  var domain = domain || "";
  document.cookie = name + "=" + escape(value) + ";path="+ path +";expires=" + expire.toGMTString() + ";domain="+ domain +";"
}
 
// 获取Cookie值
function getCookie(name) {
  var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  if (arr != null) {
    return unescape(arr[2]);
  }
  return null;
}
 
// 删除Cookie值
function removeCookie(name) {
  setCookie(name, "", -1);
}
 
// 生成范围随机数
function rand(n, m) {
  return Math.random() * (m - n) + n;
}
 
// 加入收藏夹
function addFavorite(url, title) {
  try {
    window.external.addFavorite(url, title);
  } catch(e) {
    try {
      window.sidebar.addPanel(title, url, "");
    } catch(e) {
      alert("加入收藏失败,请使用Ctrl+D进行添加");
    }
  }
}
 
// 设为首页
function setHomepage(url) {
  if (document.all) {
    document.body.style.behavior = 'url(#default#homepage)';
    document.body.setHomePage(url);
  } else if (window.sidebar) {
    if (window.netscape) {
      try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      } catch(e) {
        alert("该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
      }
    }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage', url);
  }
}
 
// 加载样式文件
function loadStyle(url) {
  try {
    document.createStyleSheet(url);
  } catch(e) {
    var cssLink = document.createElement('link');
    cssLink.rel = 'stylesheet';
    cssLink.type = 'text/css';
    cssLink.href = url;
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(cssLink)
  }
}
 
// 清除脚本内容
function stripscript(str) {
  return str.replace(/<script.*"microsoft.xmlhttp");
  xmlhttp.open("GET", url, false);
  try {
    xmlhttp.send();
  } catch(e) {
     
  } finally {
    var result = xmlhttp.responseText;
    if (result) {
      if (xmlhttp.status == 200) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
}
 
// 格式化CSS代码
function formatCss(str){
  str = str.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
  str = str.replace(/;\s*;/g, ";"); //清除连续分号
  str = str.replace(/\,[\s\.\#\d]*{/g, "{");
  str = str.replace(/([^\s])\{([^\s])/g, "$1 {\n\t$2");
  str = str.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2");
  str = str.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2");
  return str;
}
 
// 压缩CSS代码
function compressCss (str) {
  str = str.replace(/\/\*(.|\n)*"");    //删除注释
  str = str.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
  str = str.replace(/\,[\s\.\#\d]*\{/g, "{");    //容错处理
  str = str.replace(/;\s*;/g, ";");         //清除连续分号
  str = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);    //去掉首尾空白
  return (str == null) "" : s[1];
}
 
// getElementsByClassName
function getElementsByClassName(name, context) {
  var context = context || document;
  if (context.getElementsByClassName) {
    return context.getElementsByClassName(name);
  }
   
  var nodes = context.getElementsByTagName("*"), nodesLength = nodes.length, ret = [];
  for (var i = 0; i < nodesLength; i++) {
    var className = nodes[i].className;
    if (nodes[i].nodeType == 1 && className) {
      var classes = className.split(' ');
      for (var j = 0; j < classes.length; j++) {
        if (name == classes[j]) {
          ret.push(nodes[i]);
          break
        }
      }
    }
  }
  return ret;
}
 
// 获取页面高度
function getPageHeight() {
  var doc = document;
  var rot = doc.compatMode == "BackCompat" "BackCompat" "BackCompat" "BackCompat" "");
    return s && s.getPropertyValue(name);
  } else {
    return null;
  }
}
 
// 获取元素相对于这个页面的X坐标
function pageX(elem) {
  return elem.offsetParent "left"));
}
 
// 获取使用CSS定位的元素的Y坐标
function posY(elem) {
  return parseInt(getStyle(elem, "top"));
}
 
// 设置元素X位置
function setX(elem, pos) {
  elem.style.left = pos + "px";
}
 
// 设置元素Y位置
function setY(elem, pos) {
  elem.style.top = pos + "px";
}
 
// 增加元素X坐标
function addX(elem, pos) {
  set(elem, (posX(elem) + pos));
}
 
// 增加元素Y坐标
function addY(elem, pos) {
  set(elem, (posY(elem) + pos));
}
 
// 获取元素使用CSS控制大小的高度
function getHeight(elem) {
  return parseInt(getStyle(elem, "height"));
}
 
// 获取元素使用CSS控制大小的宽度
function getWidth(elem) {
  return parseInt(getStyle(elem, "width"));
}
 
// 设置透明度
function setOpacity(elem, num) {
  if (elem.filters) {
    elem.style.filter="alpha(opacity="+ num +")";
  } else {
    elem.style.opacity = num/100;
  }
}
 
// 获取鼠标光标相对于整个页面的X位置
function getX(e) {
  e = e || window.event;
  return e.pageX || e.clientX + document.body.scrollLeft;
}
 
// 获取鼠标光标相对于整个页面的Y位置
function getY(e) {
  e = e || window.event;
  return e.pageY || e.clientY + document.body.scrollTop;
}
 
// 获取鼠标光标相对于当前元素的X位置
function getElementX(e) {
  return (e && e.layerX) || window.event.offsetX;
}
 
// 获取鼠标光标相对于当前元素的Y位置
function getElementY(e) {
  return (e && e.layerY) || window.event.offsetY;
}
 
// 获取滚动条的X位置
function scrollX() {
  var de = document.documentElement;
  return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
}
 
// 获取滚动条的Y位置
function scrollY() {
  var de = document.documentElement;
  return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
}
 
// 确认是否键盘有效输入值
function checkKey(iKey) {
  if (iKey == 32 || iKey == 229) {return true;} /*空格和异常*/
  if (iKey>47 && iKey < 58)   {return true;} /*数字*/
  if (iKey>64 && iKey < 91)   {return true;} /*字母*/
  if (iKey>95 && iKey < 108)  {return true;} /*数字键盘1*/
  if (iKey>108 && iKey < 112)  {return true;} /*数字键盘2*/
  if (iKey>185 && iKey < 193)  {return true;} /*符号1*/
  if (iKey>218 && iKey < 223)  {return true;} /*符号2*/
  return false;
}
 
// 获得URL中GET参数值
function getParams() {
  var queryStr = window.location.href.split("");
  var params = [];
  if (queryStr[1]) {
    var gets = queryStr[1].split("&");
    for (var i = 0; i < gets.length; i++) {
      temp = gets.split("=");
      params[temp[0]] = temp[1];
    }
  }
  return params;
}
 
// 字符串反序
function strReverse(text) {
  return text.split('').reverse().join('');
}
 
// HTML实体
function htmlEncode(str) {
  var s = "";
  if (str.length == 0) return "";
  s = str.replace(/\&/g, "&amp;");
  s = s.replace(/</g, "&lt;");
  s = s.replace(/>/g, "&gt;");
  s = s.replace(/\'/g, "&#039;");
  s = s.replace(/\"/g, "&qout;");
  return s;
}
 
// HTML还原
function htmlDecode(str) {
  var s = "";
  if (str.length == 0) return "";
  s = str.replace(/&amp;/g, "&");
  s = s.replace(/&lt;/g, "<");
  s = s.replace(/&gt;/g, ">");
  s = s.replace(/&#039;/g, "\'");
  s = s.replace(/&qout;/g, "\"");
  return s;
}
 
// 克隆
function clone(obj) {
  var ret;
  switch (typeof obj) {
    case 'undefined':
      break;
    case 'string':
      ret = obj + '';
      break;
    case 'number':
      ret = obj - 0;
      break;
    case 'boolean':
      ret = obj;
      break;
    case 'object':
      if (obj === null) {
        ret = null;
      } else {
        if (obj instanceof Array) {
          ret = [];
          for (var i = 0, len = obj.length; i < len; i++) {
            ret.push(clone(obj[i]));
          }
        } else {
          ret = {};
          for (var k in obj) {
            ret[k] = clone(obj[k]);
          }
        }
      }
      break;
    default:
      ret = obj;
      break;
  }
  return ret;
}
 
// 检测变量是否为email格式
function isEmail(mail) {
  if (/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(mail)) {
    return true;
  }
  return false;
}
 
// 验证身份证号码
function isIdenCode(code){
  var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
  var tip = "";
  var pass = true;
  if (!code || !/^\d{6}(18|19|20)"身份证号格式错误";
    pass = false;
  } else if (!city[code.substr(0,2)]) {
    tip = "地址编码错误";
    pass = false;
  } else {
    //18位身份证需要验证最后一位校验位
    if(code.length == 18){
      code = code.split('');
      //∑(ai×Wi)(mod 11)
      //加权因子
      var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
      //校验位
      var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
      var sum = 0;
      var ai = 0;
      var wi = 0;
      for (var i = 0; i < 17; i++) {
        ai = code[i];
        wi = factor[i];
        sum += ai * wi;
      }
       
      var last = parity[sum % 11];
      if (parity[sum % 11] != code[17]) {
        tip = "校验位错误";
        pass = false;
      }
    }
  }
  return pass;
}
 
// 检测变量是否为小数
function isDecimal(dec){
  if (dec.match(/^-"";
  var len = str.length;
  for (var i = 0; i < len; i++) {
    var code = str.charCodeAt(i);
    //全角与半角相差(除空格外):65248(十进制)
    code = (code >= 0x0021 && code <= 0x007E) "";
  var len = str.length;
  for (var i = 0; i < len; i++) {
    var code = str.charCodeAt(i);
    //全角与半角相差(除空格外):65248(十进制)
    code = (code >= 0xFF01 && code <= 0xFF5E) "string" || str.length <= 0 || !(angle === 0 || angle == 1)) {
    return str;
  }
   
  var i, len, ret = [], code;
  if (angle) {
    /*半->全*/
    for (i = 0, len = str.length; i < len; i++) {
      code = str.charCodeAt(i);
      if (code == 32) {
        code = 12288;
      } else if (code < 127) {
        code += 65248;
      }
      ret.push(String.fromCharCode(code));
    }
  } else {
    /*全->半*/
    for (i = 0, len = str.length; i < len; i++) {
      code = str.charCodeAt(i);
      if (code == 12288) {
        code = 32;
      } else if (code > 65280 && code < 65375) {
        code -= 65248;
      }
      ret.push(String.fromCharCode(code));
    }
  }
  return ret.join("");
}
 
// 数据的本地化存储
function makeWebStorage() {
  //IE用userdata实现,w3c浏览器本身支持
  if (("localStorage" in window)) {
    var store = {
      set  : function(key, value) {localStorage.setItem(key, value)},
      get  : function(key)    {return localStorage.getItem(key)},
      remove : function(key)    {return localStorage.removeItem(key)}
    }
  } else {
    var store = {
      userData : null,
      name   : location.hostname,
      init   : function () {
        if (!store.userData) {
          try {
            store.userData = document.createElement('INPUT');
            store.userData.type = "hidden";
            store.userData.style.display = "none";
            store.userData.addBehavior("#default#userData");
            document.body.appendChild(store.userData);
            var expires = new Date();
            expires.setDate(expires.getDate() + 365);
            store.userData.expires = expires.toUTCString();
          } catch (e) {
            return false;
          }
        }
        return true;
      },
      setItem : function(key, value) {
        if (store.init()) {
          store.userData.load(store.name);
          store.userData.setAttribute(key, value);
          store.userData.save(store.name);
        }
      },
      getItem : function(key) {
        if (store.init()) {
          store.userData.load(store.name);
          return store.userData.getAttribute(key);
        }
      },
      remove : function(key) {
        if (store.init()) {
          store.userData.load(store.name);
          store.userData.removeAttribute(key);
          store.userData.save(store.name);
        }
      }
    };
  }
  window.webStorage = store;
}
 
function makeSessionStorage() {
  if (("sessionStorage" in window)) {
    var store = {
      set  : function(key, value) {window.sessionStorage.setItem(key, value)},
      get  : function(key)    {return window.sessionStorage.getItem(key)},
      remove : function(key)    {return window.sessionStorage.removeItem(key)}
    }
  } else {
    var store = {
      set  : function(key, value) {},
      get  : function(key)    {},
      remove : function(key)    {}
    }
  }
  window.sessStorage = store;
}

以上就是小编为大家带来的常用Javascript函数与原型功能收藏(必看篇)全部内容了,希望大家多多支持~

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?