当前位置:网站首页>Get the parameters in the URL and the interchange between parameters and objects
Get the parameters in the URL and the interchange between parameters and objects
2022-06-25 14:23:00 【jason_ renyu】
Now native h5 There are not many projects , however url There are not many operations such as parameter transfer , Here is for those in need , Offer a little help .
Don't talk much, just go to the code :
obtain URL And turn it into an object
/** * analysis url All parameter information in * @param url {String} Incoming address url, Default to the currently accessed page window.location.href * @returns {Object} return url Parameters Object Data objects , No parameter scope will be empty {} */
const getUrlParams = (url = window.location.href) => {
const searchIndex = url.lastIndexOf('?');
const search = url.substring(searchIndex + 1);
if (searchIndex < 0 || !search || (search && !/=/.test(url))) {
return {
errMsg: ' At present url The parameter is empty. ',
errHref: url
}
}
const result = search.split('&').map(s => {
if (/=/.test(s)) {
return s;
}
return s + '=';
});
const resultStr = result.join('&');
return JSON.parse(`{"${
decodeURIComponent(resultStr).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
}
Example
// Example
console.log(' Parameters in the current link :', parseQueryString());
If url No parameters , Will return to empty {}, If you don't use some js Code base or tool , To judge that the object is empty, a method is also given here , Hope to useful :
Determines if the object is null
/** * Judge whether the object is empty * @param {Object} obj The incoming object * @returns If the object is empty, return true, Is not empty is false */
const isEmptyObj = (obj) => {
if (!obj) {
return true;
}
const objKeys = Object.keys(obj);
if (objKeys.length > 0) {
return false;
}
return true;
}
Please watch carefully when using , Object is null and returns true, Don't be wrong
Object to URL Parameter splicing method of
/** * * @desc Object serialization * @param {Object} obj * @return {String} */
const stringfyQueryString = (obj) => {
if (!obj) return '';
let pairs = [];
for (let key in obj) {
let value = obj[key];
if (value instanceof Array) {
console.log(' The contents of the cycle ', value);
for (let i = 0; i < value.length; ++i) {
pairs.push(encodeURIComponent(key + '[' + i + ']') + '=' + encodeURIComponent(value[i]));
}
continue;
}
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
}
return pairs.join('&');
}
Example
// Example
const paramObj = {
name: 'zhangsan',
age: 12,
local: 'zhouer'
};
console.log(' Conversion completed ', stringfyQueryString(paramObj));
The above hope to help you
边栏推荐
- Problems encountered in debugging botu TCP connection with debugging tool
- Variables, scopes, and variable promotion
- Is qiniu regular? Is it safe to open a stock account?
- Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
- Common formatting methods for amount numbers
- How to choose a technology stack for web applications in 2022
- 一次性总结:64个数据分析常用术语!
- 哈希錶、哈希沖突
- Which is better and safer, GF easy gold rush or flush
- Shell operator
猜你喜欢

Nr-arfcn and channel grid, synchronous grid and GSCN

Experts' suggestions | 8 measures to accelerate your innovative career planning and growth

用NumPy实现神经网络(Mysteries of Neural Networks Part III)

shell 运算符

JVM 用工具分析OOM经典案例

分享自己平時使用的socket多客戶端通信的代碼技術點和軟件使用

"Mobile cloud Cup" computing power network application innovation competition is in hot registration!

shell 内置命令

112页机器学习-数学基础回顾.pptx

Solving error: creating window glfw error: glew initialization error: missing GL version
随机推荐
Is it safe to open an account with tongdaxin stock?
What are the red lines of open source that should not be trodden on?
如何在 2022 年为 Web 应用程序选择技术堆栈
shell 运算符
JS functions
Shell built-in commands
Supplementary inheritance and strict mode
Solving error: creating window glfw error: glew initialization error: missing GL version
Kubernetes cluster construction of multiple ECS
QT memory mapping
Kubernetes 理解kubectl/调试
Error1822 and error1824 are displayed in the database
As a software testing engineer, how do you think to ensure software quality?
Is it safe for Guosen Securities to open a stock account? Excuse me?
【世界历史】第二集——文明的曙光
Kubernetes cluster construction of multiple ECS
Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
关于STM32使用LAN8720A插拔网线重连
完整详细的汇编实验报告
Shell operator