最推荐的方式是使用urlsearchparams对象。1. 使用new urlsearchparams(window.location.search)创建实例来获取当前url的查询参数;2. 通过get('key')获取单个参数值;3. 使用getall('key')获取同名参数的所有值组成的数组;4. 用has('key')检查参数是否存在;5. 通过entries()遍历所有参数;6. 对于任意url字符串,可用new url(urlstring)解析,并通过其searchparams属性操作查询参数;7. urlsearchparams自动处理编码解码,无需手动调用decodeuricomponent或encodeuricomponent,确保参数值正确解析和生成,极大提升开发效率与代码健壮性。

在JavaScript里,想获取URL的查询参数,最直接也最推荐的方式就是使用
URLSearchParams
要获取当前URL的查询参数,你可以这样做:
// 假设当前URL是:https://example.com/page?id=123&name=test&tags=js&tags=web
const urlParams = new URLSearchParams(window.location.search);
// 获取单个参数值
const id = urlParams.get('id'); // '123'
const name = urlParams.get('name'); // 'test'
console.log(`ID: ${id}, Name: ${name}`);
// 获取所有同名参数的值(返回数组)
const tags = urlParams.getAll('tags'); // ['js', 'web']
console.log(`Tags: ${tags.join(', ')}`);
// 检查是否存在某个参数
const hasId = urlParams.has('id'); // true
const hasCategory = urlParams.has('category'); // false
console.log(`Has ID: ${hasId}, Has Category: ${hasCategory}`);
// 遍历所有参数
console.log('所有参数:');
for (const [key, value] of urlParams.entries()) {
console.log(`${key}: ${value}`);
}
// 你也可以从一个完整的URL字符串中解析参数
const specificUrl = 'https://another.com/path?param1=value1&param2=value2';
const specificUrlParams = new URLSearchParams(new URL(specificUrl).search);
const param1 = specificUrlParams.get('param1'); // 'value1'
console.log(`Specific URL Param1: ${param1}`);说实话,以前我们没有
URLSearchParams
window.location.search
split('&')split('=')URLSearchParams
它之所以成为首选,原因非常多:
URLSearchParams
encodeURIComponent
decodeURIComponent
get()
getAll()
has()
?tag=js&tag=css
URLSearchParams
getAll()
for...of
在我看来,它就是那种“一旦用了就回不去”的工具,极大提升了开发效率和代码健壮性。
当URL中出现像
?keyword=apple&keyword=iphone
keyword
URLSearchParams
get(name)
const params = new URLSearchParams('?keyword=apple&keyword=iphone&color=red');
const keyword = params.get('keyword');
console.log(keyword); // 输出: "apple"如果你只关心第一个值,或者你知道参数不会重复,那么
get()
getAll(name)
const params = new URLSearchParams('?keyword=apple&keyword=iphone&color=red');
const keywords = params.getAll('keyword');
console.log(keywords); // 输出: ["apple", "iphone"]这是处理多值参数的正确姿势。比如一个商品可能有多个标签(tags),或者一个搜索可能有多个筛选条件,用
getAll()
我个人在写代码时,如果我不确定参数是否会重复,或者我知道它可能重复且需要所有值时,我一定会优先使用
getAll()
编码问题在URL处理中是个老大难。比如URL中包含中文、空格、特殊符号(
&
=
decodeURIComponent()
%E4%BD%A0%E5%A5%BD
你好
%20
URLSearchParams
get()
getAll()
decodeURIComponent
// 假设URL是:https://example.com/search?q=%E4%BD%A0%E5%A5%BD%20%E4%B8%96%E7%95%8C&category=IT%2FWeb
const params = new URLSearchParams(window.location.search);
const query = params.get('q');
console.log(query); // 输出: "你好 世界" (已经自动解码)
const category = params.get('category');
console.log(category); // 输出: "IT/Web" (斜杠也正常显示)你看,我们完全不需要手动去调用
decodeURIComponent
append()
set()
URLSearchParams
encodeURIComponent
虽然我们主要讨论了查询参数,但一个完整的URL还有很多其他部分,比如协议、域名、路径、哈希(锚点)等。在JavaScript中,
window.location
URL
使用 window.location
// 假设当前URL是:https://www.example.com:8080/path/to/page.html?id=123#section1 console.log(window.location.href); // 完整的URL: "https://www.example.com:8080/path/to/page.html?id=123#section1" console.log(window.location.protocol); // 协议: "https:" console.log(window.location.host); // 主机名和端口: "www.example.com:8080" console.log(window.location.hostname); // 主机名: "www.example.com" console.log(window.location.port); // 端口: "8080" console.log(window.location.pathname); // 路径: "/path/to/page.html" console.log(window.location.search); // 查询字符串: "?id=123" (这就是我们前面URLSearchParams的来源) console.log(window.location.hash); // 哈希/锚点: "#section1"
这些属性都非常有用,比如你想根据路径判断当前页面类型,或者根据哈希值进行页面内导航。
使用 URL
URL
URL
window.location
const someUrlString = 'http://api.example.com/users/123?action=view&token=abc#profile';
const urlObject = new URL(someUrlString);
console.log(urlObject.protocol); // "http:"
console.log(urlObject.hostname); // "api.example.com"
console.log(urlObject.pathname); // "/users/123"
console.log(urlObject.search); // "?action=view&token=abc"
console.log(urlObject.hash); // "#profile"
// 甚至可以直接访问它的 searchParams 属性,它就是一个 URLSearchParams 实例
const action = urlObject.searchParams.get('action'); // "view"
console.log(action);这个
URL
URL
以上就是js如何获取url的查询参数的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号