JS-js的部分基础方法实现整理

获取URL Query

export const getHashUrlQuery = (name) => {
    let reg = new RegExp('(^|&)' + name + '=([^&#]*)(&|#|$)', 'i'),
        url = window.location.href,
        r = url.substr(1).match(reg)
    if (r != null) {
        return unescape(r[2])
    } else {
        return ''
    }
}
export const getUrlQuery = (name, Url) => {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    let r = window.location.search.substr(1).match(reg)
    if (r != null) {
        return decodeURI(r[2])
    } else {
        return ''
    }
}

cookie存取

export const readCookie = function (name) {
    let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
    let arr = document.cookie.match(reg)
    if (arr) {
        return unescape(arr[2])
    } else {
        return ''
    }
}
export const createCookie = function (name, value, expires) {
    let exp = new Date()
    exp.setTime(exp.getTime() + Number(expires) * 1000)
    document.cookie = `${name}=${value};expires=${exp.toGMTString()};domain=${domain};path=/`
}
export const eraseCookie = function (name) {
    createCookie(name, '', -1)
}

localStorage的存取

export const setLocalStore = (name, content) => {
    if (!name) return
    if (typeof content !== 'string') {
        content = JSON.stringify(content)
    }
    window.localStorage.setItem(name, content)
}
export const getLocalStore = name => {
    if (!name) return
    return window.localStorage.getItem(name)
}
export const removeLocalStore = name => {
    if (!name) return
    window.localStorage.removeItem(name)
}

sessionStorage的存取

export const setSessionStore = (name, content) => {
    if (!name) return
    if (typeof content !== 'string') {
        content = JSON.stringify(content)
    }
    window.sessionStorage.setItem(name, content)
}

export const getSessionStore = name => {
    if (!name) return
    return window.sessionStorage.getItem(name)
}

export const removeSessionStore = name => {
    if (!name) return
    window.sessionStorage.removeItem(name)
}

随机数

function randomNum(n) {
    let rnd = "";
    for (let i = 0; i < n; i++) {
        rnd += Math.floor(Math.random() * 10);
    }
    return rnd;
}

是否IE浏览器

export const IsIE = function () {
  let userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
  let isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 // <ie11
  let isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1 // =ie11
  return (isIE || isIE11)
}

获取当前时间

// 获取当前时间(如:2009-06-12 12:00)
export const getCurentTime = function () {
  let now = new Date()
  let year = now.getFullYear()       //年
  let month = now.getMonth() + 1     //月
  let day = now.getDate()            //日
  let hh = now.getHours()            //时
  let mm = now.getMinutes()          //分
  let clock = year + '-'
  if (month < 10) {
    clock += '0'
  }
  clock += month + '-'
  if (day < 10) {
    clock += '0'
  }
  clock += day + ' '
  if (hh < 10) {
    clock += '0'
  }
  clock += hh + ':'
  if (mm < 10) {
    clock += '0'
  }
  clock += mm
  return (clock)
}

正则表达式用法

正则表达式 .test(字符串)
字符串.match(正则表达式)

export const getDevice = () => {
    let ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i)) {
        return 'wx'
    } else if (/iphone|ipad|ipod/.test(ua)) {
        return 'ios'
    } else if (/android/.test(ua)) {
        return 'android'
    }
}

JS进行页面跳转的学习

https://www.cnblogs.com/lijshui/p/7451360.html
https://www.cnblogs.com/liumengdie/p/7918601.html

猜你喜欢

转载自blog.csdn.net/long870294701/article/details/84447721