js常用方法,JS实用方法,jq获得文件后缀,解析window。location,解析URL参数

jq获取文件后缀的方法

var file = "测试文档1111111111.docx"; // 文件
var fileName = file.replace(/.*(\/|\\)/, ""); // 获得文件名
var fileSuffix = (/[.]/.exec(fileName)) ? /[^.]+$/.exec(fileName.toLowerCase()) : ''; // 获得文件后缀
var file = "测试文档1111111111.docx";
var dot = file.lastIndexOf(".");
var type = file.substr(dot);
var fileSuffix = "";
if(type === ".jpg" || type === ".gif" || type === ".JPG" || type === ".GIF" || type === ".pdf" || type === ".docx" || type === ".doc"){
    fileSuffix = type;
}

jq解析window.location

console.log(window.location.href) // 当前URL;例子:https://baijiahao.baidu.com/s?id=1625846473808878131&wfr=spider&for=pc
console.log(window.location.protocol) // 协议;例子:https:
console.log(window.location.hostname) // 域名;例子:baijiahao.baidu.com
console.log(window.location.origin) // 协议+域名;例子:https://baijiahao.baidu.com
console.log(window.location.search) // 请求参数;例子:?id=1625846473808878131&wfr=spider&for=pc

jq解析URL参数

function getParameter(name) {
    // 正则:[找寻'&' + 'url参数名字' = '值' + '&']'&'可以不存在)
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if(r != null) {
      // 对参数值进行解码
        return unescape(r[2]); 
    }
    return null;
}
 
// 调用方法,注意需要传入String类型的数据,输出结果为String类型
getParameter('id');   // '123'
// 使用正则表达式来匹配全路径下的?后的参数,避免#的干扰
const getUrlValue = function (name) {
  let href = location.href
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || [, ""])[1].replace(/\+/g, '%20')) || null
}
 
getUrlValue('inApp')
/**
 * @param name 获取参数名
 * 调用方法,注意需要传入String类型的数据,输出结果为String类型
 * getUrlParameter('id') // '123'
 */
const getUrlParameter = function (name) {
  // 正则:[找寻'&' + 'url参数名字' = '值' + '&']('&'可以不存在)
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  let r = window.location.search.substr(1).match(reg)
  if (r != null) {
    // 对参数值进行解码
    return unescape(r[2])
  }
  return null
}

/**
* 解析URL传参
* @param {Object} key
*/
const getQueryString = function (key) {
  var after = window.location.search
  // 如果url中没有传参直接返回空
  if (after.indexOf('?') === -1) {
    return ''
  }
  // key存在先通过search取值如果取不到就通过hash来取
  after = after.substr(1) || window.location.hash.split('?')[1]
  if (after) {
    var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)')
    var r = after.match(reg)
    if (r != null) {
      return decodeURIComponent(r[2])
    } else {
      return ''
    }
  }
}

// 使用正则表达式来匹配全路径下的?后的参数
const getUrlValue = function (name) {
  let href = location.href
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || ['', ''])[1].replace(/\+/g, '%20')) || null
}

const getUrlParse = function () {
  let url = window.location.search
  let obj = {}
  let reg = /[?&][^?&]+=[^?&]+/g // 正在表达式
  let arr = url.match(reg)
  // 得到数组:['?id=12345', '&a=b']
  if (arr) {
    arr.forEach(item => {
      let tempArr = item.substring(1).split('=')
      let key = decodeURIComponent(tempArr[0])
      let val = decodeURIComponent(tempArr[1])
      obj[key] = val
    })
  }
  return obj
}

const getUrlParses = function () {
  let url = window.location.href.split('#')[1]
  console.log(window.location.href)
  let querys = url.substring(url.indexOf('?') + 1).split('&')
  // let href = window.location.href;
  // if (href.indexOf('groupmessage') > -1 || href.indexOf('singlemessage') > -1 || href.indexOf('timeline') > -1) {
  //   href = href.replace(/\?from=(groupmessage|singlemessage|timeline)(\S*)#/, '#');
  //   console.log("getUrlParses -> href", href)
  //   // window.location.href = href;
  // }
  let result = {}
  // 如果url中没有传参直接返回空
  if (url.indexOf('?') === -1) {
    return {}
  }
  for (let i = 0; i < querys.length; i++) {
    let temp = querys[i].split('=')
    if (temp.length < 2) {
      result = {}
    } else {
      result[temp[0]] = temp[1]
    }
  }
  return result
}

// export default getUrlParameter
export {
  getUrlParameter,
  getQueryString,
  getUrlValue,
  getUrlParse,
  getUrlParses
}

获取url参数

// $.getUrlValue('id')
getUrlValue(name) {
  let href = location.href
  return (
    decodeURIComponent(
      (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || ['', ''])[1].replace(
        /\+/g,
        '%20'
      )
    ) || null
  )
},

删除URL中的某个参数

// $.delParamsQuery('id')
delParamsQuery(name) {
  var loca = window.location;
  var baseUrl = loca.origin + loca.pathname + loca.hash.substring(0, loca.hash.indexOf('?') + 1);
  var query = loca.hash.substring(loca.hash.indexOf('?') + 1);
  if (query.indexOf(name) > -1) {
    var obj = {}
    var arr = query.split("&");
    for (var i = 0; i < arr.length; i++) {
      arr[i] = arr[i].split("=");
      obj[arr[i][0]] = arr[i][1];
    };
    delete obj[name];
    var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
    return url
  } else {
    return window.location.href;
  };
},

判断是否在微信

// $.isWX()  true or false
isWX() {
 let wxInfo = navigator.userAgent.toLowerCase() // 判断是否在微信中打开
  if (wxInfo.match(/MicroMessenger/i) == 'micromessenger') {
    window.localStorage.setItem('isWX', true)
    return true
  } else {
    window.localStorage.setItem('isWX', false)
    return false
  }
}

数组拼接

let union = a.concat(b)

修改修改对象中的数据

// 修改对象中的数据
this.setData({
   ['prizeList[' + 4 + '].prizePic']: 'url'
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值