js: get parameters from link

Knownurl=http://192.168.31.248:8080/#/pages/index?id=1&name=2&code=3
Want to get the following data

{
    
    
	id:1,
	name:2,
	code:3
}

Implementation

var fullURL = window.location.href; // 获取完整的 URL,包括哈希部分
var hash = fullURL.split("index")[1]; // 从完整 URL 中提取哈希部分
var params = new URLSearchParams(hash); // // 使用 URLSearchParams 对象解析哈希中的查询参数
var parsedParams = {
    
    }; // 创建一个空的对象,用于存储解析后的参数

// 遍历查询参数,将其存储到对象中
params.forEach(function(value, key) {
    
    
	parsedParams[key] = value;
});
console.log(parsedParams);

Guess you like

Origin blog.csdn.net/qq_40745143/article/details/134159632