指定 url 或 当前 url 的基本处理
不校验网址合法性,默认你传过来的都是正常的网址
通用代码
srcutilurlUtil.js
/**
* 获取当前url或者指定url 中的 params 对象
* @param {String} wholeUrl 完整的 url
*/
export function getQueryParamsObj(wholeUrl) {
let params = null;
let url = wholeUrl ? wholeUrl : window.location.href;
let queryParams = url.split("?");
queryParams = Array.isArray(queryParams) && queryParams[1] ? queryParams[1] : "";
params = {};
var vars = queryParams.split("&");
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split("=");
params[pair[0]] = pair[1];
}
return params;
}
/**
* 获取指定url或者当前url不携带参数的地址
* @param {String} wholeUrl 完整的 url 字符串
*/
export function getUrlNoQueryParams(wholeUrl) {
var url = wholeUrl ? wholeUrl : window.location.href
if (url.indexOf('?') > 0) {
var baseUrl = url.substring(0, url.indexOf('?'))
return baseUrl
}
return url
}
/**
* 获取指定url或者当前url中的某个查询参数,没值返回 false(该参数的值可能也正好是 false,需自行注意)
* @param {String} paramName 需要获取到的 param 名字
* @param {String} wholeUrl 完整的 url 字符串
*/
export function getQueryParamValue(paramName, wholeUrl) {
var url = wholeUrl ? wholeUrl : window.location.href
var queryStr = url.substring(url.indexOf('?') + 1);
var params = queryStr.split("&");
for (var i = 0; i < params.length; i++) {
var pair = params[i].split("=");
if (pair[0] == paramName) { return pair[1]; }
}
return (false);
}
/**
* 删除当前url或指定url的指定 params 参数,并返回新的 url
* @param {String} paramName 需要删除的 param 名
* @param {String} wholeUrl 需要修改的 url 地址
*/
export function deleteQueryParamValue(paramName, wholeUrl) {
var url = wholeUrl ? wholeUrl : window.location.href
var paramStr = url.substring(url.indexOf('?') + 1)
var baseUrl = url.substr(0, url.indexOf("?"))
var paramsUrl = ""
var arr = new Array()
if (paramStr != "") {
var params = paramStr.split("&")
for (var i = 0; i < params.length; i++) {
var pair = params[i].split("=")
if (pair[0] != paramName) {
arr.push(params[i])
}
}
}
if (arr.length > 0) {
paramsUrl = "?" + arr.join("&")
}
url = baseUrl + paramsUrl
return url
}
使用案例
① 引入js文件
② 调用方法
import { getQueryParamsObj, getUrlNoQueryParams, getQueryParamValue, deleteQueryParamValue } from '../../util/urlUtil'
// ----- getQueryParamsObj ---------
getQueryParamsObj() // 当前页面 url => https://www.baidu.com/s?ie=UTF-8&wd=localstorage
// {ie: "UTF-8", wd: "localstorage"}
getQueryParamsObj('https://www.baidu.com/s?ie0=UTF-8&wd=localstorage&a=1')
// {ie0: "UTF-8", wd: "localstorage", a: "1"}
// ----- getUrlNoQueryParams ---------
getUrlNoQueryParams() // 当前页面 url => https://www.baidu.com/s?ie=UTF-8&wd=localstorage
// "https://www.baidu.com/s"
getUrlNoQueryParams('https://www.baidu.com/s?ie0=UTF-8&wd=localstorage&a=1')
// "https://www.baidu.com/s"
// ----- getQueryParamValue ---------
getQueryParamValue('wd') // 当前页面 url => https://www.baidu.com/s?ie=UTF-8&wd=localstorage
// "localstorage"
getQueryParamValue('wd', 'https://www.baidu.com/s?ie0=UTF-8&wd=ssesionstorage&a=1')
// "ssesionstorage"
// ----- deleteQueryParamValue ---------
deleteQueryParamValue('wd') // 当前页面 url => https://www.baidu.com/s?ie=UTF-8&wd=localstorage
// "https://www.baidu.com/s?ie=UTF-8"
deleteQueryParamValue('wd', 'https://www.baidu.com/s?ie0=UTF-8&wd=ssesionstorage&a=1')
// "https://www.baidu.com/s?ie0=UTF-8&a=1"