mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-21 14:34:06 +01:00
改进 getSearch 函数实现 (#7094)
* 🎨 改进 `getSearch` 方法 兼容 key 为空字符串与 value 为空字符串的 URL search params * 🎨 改进 `getSearch` 方法 * 🎨 style * 🎨 改进 `getSearch` 方法 兼容 `getSearch("page", "assets/第13章 编程陷阱-20220524015630-wd53jod.pdf?page=12")` REF: #7089 * 🎨 改进 `getSearch` 方法 * `getSearch` 返回值兼容 `null` 与 `""`
This commit is contained in:
parent
f0d9a3a7ab
commit
30a5cdc9f5
1 changed files with 7 additions and 14 deletions
|
|
@ -14,20 +14,13 @@ export const getRandom = (min: number, max: number) => {
|
|||
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
|
||||
};
|
||||
|
||||
export const getSearch = (key: string, link = window.location.search) => {
|
||||
if (link.indexOf("?") === -1) {
|
||||
return "";
|
||||
}
|
||||
let value = "";
|
||||
const data = link.split("?")[1].split("&");
|
||||
data.find(item => {
|
||||
const keyValue = item.split("=");
|
||||
if (keyValue[0] === key) {
|
||||
value = keyValue[1];
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return value;
|
||||
export const getSearch: (key: string, link?: string) => string | null = (key: string, link = window.location.search) => {
|
||||
const params = link.substring(link.indexOf('?'));
|
||||
const hashIndex = params.indexOf('#');
|
||||
const searchParams = params.substring(0, hashIndex >= 0 ? hashIndex : undefined);
|
||||
// REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
|
||||
const urlSearchParams = new URLSearchParams(searchParams);
|
||||
return urlSearchParams.get(key);
|
||||
};
|
||||
|
||||
export const isBrowser = () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue