Files
it-console/code/ts/后端辅助工具/src/utils/url.ts
2023-03-01 16:08:00 +08:00

77 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: Kane
* @Date: 2023-02-28 19:30:40
* @LastEditors: Kane
* @FilePath: /后端辅助工具/src/utils/url.ts
* @Description:
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
/**
* 将url的参数拆分成对象
* @param url 访问的url
* @returns
*/
function getURLParams(url: string)
{
const arr = url.split("?");
const params = arr[1].split("&");
const obj = {};
for (let i = 0; i < params.length; i++)
{
const param = params[i].split("=");
obj[param[0]] = param[1];
}
return obj;
}
function getParamsFromURL(url: string)
{
const indexOfQuestionMark: number = url.indexOf("?");
const indexOfSharp: number = url.indexOf("#");
const paramObj = {};
let paramString;
//url中没有问号说明没有参数
if (indexOfQuestionMark < 0)
{
return paramObj;
}
//检查是否有#号
if (indexOfSharp < 0)
{
//没有#号,可以直接截取参数字符串
paramString = url.substring(indexOfQuestionMark);
}
else
{
//有#号,需要排除掉#的内容
const end: number = indexOfQuestionMark < indexOfSharp ? indexOfSharp : url.length;
paramString = url.substring(indexOfQuestionMark + 1, end);
}
//拆分属性
const paramArray: string[] = paramString.split("&");
paramArray.forEach((item) =>
{
if (item.length == 0)
{
return;
}
const param = item.split("=");
paramObj[param[0]] = param[1] || "";
});
return paramObj;
}
export { getURLParams, getParamsFromURL };