使用 JavaScript 更改 URL 参数并指定默认值 [英] Change URL parameters and specify defaults using JavaScript
本文介绍了使用 JavaScript 更改 URL 参数并指定默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个网址:
site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc
我需要的是能够将 'rows' url 参数值更改为我指定的值,比如说 10.如果 'rows' 不存在,我需要将它添加到 url 的末尾并添加我已经指定的值 (10).
what I need is to be able to change the 'rows' url param value to something i specify, lets say 10. And if the 'rows' doesn't exist, I need to add it to the end of the url and add the value i've already specified (10).
推荐答案
4 年后,在学到了很多东西之后,回答我自己的问题.尤其是您不应该对所有事情都使用 jQuery.我创建了一个可以解析/字符串化查询字符串的简单模块.这使得修改查询字符串变得容易.
To answer my own question 4 years later, after having learned a lot. Especially that you shouldn't use jQuery for everything. I've created a simple module that can parse/stringify a query string. This makes it easy to modify the query string.
您可以使用 query-string 如下:
// parse the query string into an object
var q = queryString.parse(location.search);
// set the `row` property
q.rows = 10;
// convert the object to a query string
// and overwrite the existing query string
location.search = queryString.stringify(q);
这篇关于使用 JavaScript 更改 URL 参数并指定默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文