7

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: (c is "h" instead of "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.

How am I going to do this?

0

8 Answers 8

12

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, '$1=newValue');

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

/([?&]z)=([^#&]*)/g; must be used instead of /([?&]z)=([^&]*)/g;, because it else might override the fragment.
1

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

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

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

Easy.

3 Comments

Doesn't this lead to problems if there is a fragment being used? (For example, http://example.org/file?param1=value1#abc&c=h is a invalid URL.
It also leads to a invalid URL if no query string is being used before. Example: http://example.org/file.php&c=h is a invalid URL.
@Zulakis True, you have to be more intelligent with the way this is done.
1

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

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}

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.location to move the user to next page.

Comments

0

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];

Comments

0

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&")

Comments

0

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

Comments

0

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

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, "$1" + newParam);
        } else { // Otherwise, add it to end of query string
            params = urlQueryString + '&' + newParam;
        }
    }
    window.history.replaceState({}, "", baseUrl + params);
};

Comments

-1

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

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

1 Comment

although it's very basic, my head skipped straight over this method but is the only thing that would work for what im doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.