javascript jquery修改url获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13551747/
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 19:07:54  来源:igfitidea点击:

jquery modify url get parameters

javascriptjqueryhttpget

提问by Zulakis

I got a URL like this:

我得到了一个这样的网址:

http://google.de/test.php?a=b&c=d&e=f

I know got to modify just one of the GET params like this: (cis "h" instead of "d")

我知道只需要修改这样的 GET 参数之一:(c是“h”而不是“d”)

http://google.de/test.php?a=b&c=h&e=f

and redirect to the new url. All other GET params should stay the same.

并重定向到新的 url。所有其他 GET 参数应保持不变。

How am I going to do this?

我该怎么做?

回答by BuddhiP

I agree its best to use a library for this purpose as mentioned in other answers.

我同意最好为此目的使用图书馆,如其他答案中所述。

However, here is a string replacement solution based on regular expressions if you need a simpler quick fix.

但是,如果您需要更简单的快速修复,这里有一个基于正则表达式的字符串替换解决方案。

var url = "http://my.com/page?x=y&z=1&w=34";

var regEx = /([?&]z)=([^#&]*)/g;
var newurl = url.replace(regEx, '=newValue');

Here I'm replacing the query string key 'z' with 'newVlaue'

在这里,我将查询字符串键 'z' 替换为 'newVlaue'

Have a look at this fiddle: http://jsfiddle.net/BuddhiP/PkNsx

看看这个小提琴:http: //jsfiddle.net/BuddhiP/PkNsx

回答by dm03514

as mentioned window.location.searchwill give you the query string with ?included.

如前所述,window.location.search将为您提供?包含的查询字符串。

I would then turn these into an object (perhaps with a library like http://github.com/medialize/URI.js) to make it easier to work with the params. like var params = { key: value, key1: value1}

然后我会将它们转换为一个对象(也许使用像http://github.com/medialize/URI.js这样的库),以便更轻松地使用参数。喜欢var params = { key: value, key1: value1}

You could then modify the value you wanted and convert your key value pairs back to a query string.

然后您可以修改您想要的值并将您的键值对转换回查询字符串。

After this you could use window.locationto move the user to next page.

在此之后,您可以使用window.location将用户移动到下一页。

回答by Christian

A little known feature of PHP is that a latter naming of an existing parameter overrides the earlier.

PHP 的一个鲜为人知的特性是现有参数的后一个命名覆盖了先前的命名。

With this knowledge at hand, you can just do the following:

掌握这些知识后,您只需执行以下操作:

location.href = location.href + '&c=h';

Easy.

简单。

回答by wilfredonoyola

Set or Update a URL/QueryString Parameter, and update URL using HTML history.replaceState()

设置或更新 URL/QueryString 参数,并使用 HTML history.replaceState() 更新 URL

You can try something like this:

你可以尝试这样的事情:

var updateQueryStringParam = function (key, value) {
    var baseUrl = [location.protocol, '//', location.host, location.pathname].join(''),
        urlQueryString = document.location.search,
        newParam = key + '=' + value,
        params = '?' + newParam;

    // If the "search" string exists, then build params from it
    if (urlQueryString) {
        keyRegex = new RegExp('([\?&])' + key + '[^&]*');

        // If param exists already, update it
        if (urlQueryString.match(keyRegex) !== null) {
            params = urlQueryString.replace(keyRegex, "" + newParam);
        } else { // Otherwise, add it to end of query string
            params = urlQueryString + '&' + newParam;
        }
    }
    window.history.replaceState({}, "", baseUrl + params);
};

回答by Narendra Rajput

Since your parameter can be at any place. So this will work fine

因为你的参数可以在任何地方。所以这会正常工作

url="http://google.de/test.php?a=b&c=d&e=f";
url=url.replace(/&c=.*&/,"&c=h&")

回答by bashleigh

I've put together a really simple method but it will only work with what you're trying to achieve:

我整理了一个非常简单的方法,但它只适用于您想要实现的目标:

var url = 'http://google.de/test.php?a=b&c=d&e=f';
var gets =  url.split('.php?');
var ge = gets[1].split('&');
var change = ge[1].split('=');
var change[1] = 'h';
var newUrl = url[0] + '.php?' + ge[0] + '&' + change[0] + '=' + change[1] + '&' + ge[2];

回答by Viren Rajput

You can try something like this:

你可以尝试这样的事情:

var changed = "h"; // you can vary this GET parameter

var url = "http://google.de/test.php?a=b&e=f&c=" + changed; // append it to the end

window.location = newurl; // redirect the user to the url

回答by Romain

location.href = location.origin+location.pathname+location.search;

Replace location.search by your new arguments like "?foo=bar&tic=tac"

用你的新参数替换 location.search,比如“?foo=bar&tic=tac”