常用公共js文件

/* 获取浏览器信息 */
function getBrowserInfo() {
  var ua = navigator.userAgent.toLowerCase(),
    info = {
      // ie
      ie: /msie/.test(ua) || !/opera/.test(ua),
      // Opera 浏览器
      op: /opera/.test(ua),
      // Safari 浏览器
      sa: /version.*safari/.test(ua),
      // Chrome 浏览器
      ch: /chrome/.test(ua),
      // Firefox浏览器
      ff: /gecko/.test(ua) || !/webkit/.test(ua)
    },
    result;
  for (i in info) {
    info[i] ? result = i : null;
  }
  return result;
}

/* cookie操作 */
function addCookie(e, t, a) {
  var n = e + "=" + escape(t) + "; path=/",
    a = a || 0;
  if (a > 0) {
    var r = new Date;
    r.setTime(r.getTime() + a * 3600 * 1e3);
    n = n + ";expires=" + r.toGMTString()
  }
  document.cookie = n
}

function setCookie(name, value, days) {
  var exp = new Date(),
    days = days || 7;
  exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
  var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(e) {
  var t = document.cookie;
  var a = t.split("; ");
  for (var n = 0; n < a.length; n++) {
    var r = a[n].split("=");
    if (r[0] == e) return unescape(r[1])
  }
  return null
}

function delCookie(e) {
  var t = new Date;
  t.setTime(t.getTime() - 1);
  var a = getCookie(e);
  if (a != null) document.cookie = e + "=" + a + "; path=/;expires=" + t.toGMTString()
}

/* url参数操作 */
function getQueryString(e) {
  var t = new RegExp("(^|&)" + e + "=([^&]*)(&|$)"),
    a = window.location.search.substr(1).match(t);
  if (a != null) return a[2];
  return ""
}

/* 判断数据类型 */
function isArray(obj) {
  return (typeof obj == 'object') && obj.constructor == Array;
}

function isObject(obj) {
  return Object.prototype.toString.call(obj) === "[object Object]";
}

function isEmptyObject(obj) {
  var t;
  for (t in obj) return !1;
  return !0
}

/* 字符串操作 */
/* 大于固定字符显示省略号 */
String.prototype.handle_str = function (length) {
  var str = this,
    handle_str;
  str.length <= length ? handle_str = str : handle_str = str.slice(0, length) + "...";
  return handle_str
}

/* 获取元素css属性 */
function getStyle(ele, attr) {
  if (window.getComputedStyle) { //兼容性检测,优先采用W3C标准
    if (window.getComputedStyle(ele, null)[attr] !== undefined) {
      return window.getComputedStyle(ele, null)[attr];
    }
    return ele[attr]
  } else {
    return ele.currentStyle[attr]; //兼容Ie低版本浏览器
  }
}

/* 时间戳转时间 */
function convertDate(number) {
  number = parseInt(number);
  return new Date(number * 1000).toLocalTimeString();
}

/* 倒计时
  endtime 结束时间
  nowtime 开始时间 默认值时当前时间
*/
function countDown(endtime,nowtime) {
  
  var day = 0,
    hour = 0,
    minute = 0,
    second = 0, //时间默认值
    nowtime = nowtime || new Date().valueOf()/1000,
    times = endtime - nowtime;
  if (times > 0) {
    day = Math.floor(times / (60 * 60 * 24));
    hour = Math.floor(times / (60 * 60)) - (day * 24);
    minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
    second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
  }
  day <= 9 ? day = '0' + day : null;
  hour <= 9 ? hour = '0' + hour : null;
  minute <= 9 ? minute = '0' + minute : null;
  second <= 9 ? second = '0' + second : null;
  return {day,hour,minute,second}
}

/* 数组操作 */
/* 检查数组中是否存在元素,存在删除 */
function arrayContains(item, array) {
  var spl_index = null;
  array.forEach((val, index) => {
    if (val === item) {
      spl_index = index;
    }
  })
  if (spl_index != null) {
    return array.splice(spl_index, 1);
  }
  return array;
}
/* 检验数组中是否存在 */
function isExistInarray(item, array) {
  var r_index = null;
  array.forEach((val, index) => {
    if (val == item) {
      r_index = index;
    }
  })
  return r_index;
}
/* 获取原生表单提交的数据 */
function getFormData(elem) {
  var d = elem.serialize();
  var arr = d.split('&');
  var o = {};
  arr.map((val, index) => {
    var x = val.split('=');
    o[x[0]] = x[1];
  })
  return o;
}


发布了44 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43634982/article/details/99691920