javascript 使用 jQuery 获取/更改/删除 URL 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9009311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:18:45  来源:igfitidea点击:

get/change/remove URL parameters with jQuery?

javascriptjqueryurl

提问by Alex

I was looking over this questionand searching google but I didn't find any update, so I am wondering if any of you know anything more recent because the last update on

我正在查看这个问题并在 google 上搜索,但我没有找到任何更新,所以我想知道你们中是否有人知道最近的任何更新,因为上次更新是

https://github.com/blairmitchelmore/jquery.pluginswas in 2009 and 2010 on https://github.com/cowboy/jquery-bbq

https://github.com/blairmitchelmore/jquery.plugins是在 2009 年和 2010 年在https://github.com/cowboy/jquery-bbq

Or any other ideas? I need to add/change/remove parameters to my url

还是有其他想法?我需要向我的 url 添加/更改/删除参数

回答by Shiplu Mokaddim

Its easy to do by pure JS.

纯JS很容易做到。

See this code from www.samaxes.com

www.samaxes.com查看此代码

var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';
location.search = $.param(queryParameters);

Its not flawless. But at least it can give you some idea.

它并非完美无缺。但至少它可以给你一些想法。

Update 1:
Hereis a function I wrote for another answer (cant remember). It works perfect.

更新 1:
是我为另一个答案编写的函数(不记得了)。它工作完美。