1. 程式人生 > >JS-js的部分基礎方法實現整理

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