Change Url Parameters and Specify Defaults Using JavaScript

Change URL parameters and specify defaults using JavaScript

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.

You can use query-string as follows:

// 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);

Change URL parameters and specify defaults using JavaScript

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.

You can use query-string as follows:

// 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);

How to replace url parameter with javascript/jquery?

Wouldn't this be a better solution?

var text = 'http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100';
var newSrc = 'www.google.com';
var newText = text.replace(/(src=).*?(&)/,'$1' + newSrc + '$2');

EDIT:

added some clarity in code and kept 'src' in the resulting link

$1 represents first part within the () (i.e) src= and $2 represents the second part within the () (i.e) &, so this indicates you are going to change the value between src and &. More clear, it should be like this:

src='changed value'& // this is to be replaced with your original url

ADD-ON for replacing all the ocurrences:

If you have several parameters with the same name, you can append to the regex global flag, like this text.replace(/(src=).*?(&)/g,'$1' + newSrc + '$2'); and that will replaces all the values for those params that shares the same name.

Set default value for URL parameter in HTTP GET in node.js

If just want to check if something is provided, then you could just do:

var ZZZ = req.query.ZZZ || 111;

But... GET parameters are query strings, so we probably want to make sure it is a number.

if (!parseInt(req.query.ZZZ)) {
req.query.ZZZ = 111;
}

Or if you want to get ternary with it:

req.query.ZZZ = parseInt(req.query.ZZZ) ? req.query.ZZZ : 111;

Do note that the other parameters are a string and that this default is being set as a number. So, you might want '111' as opposed to 111. Also, you can parseInt all of your query strings or toString them all if they are all a number, just try to make sure they all remain the same expected type. Unless of course these are all strings of text, in which case ignore all this.

How can I add or update a query string parameter?

I wrote the following function which accomplishes what I want to achieve:

function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}

Add / Change parameter of URL and redirect to the new URL

function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName + "="));
var suffix = url.substring(url.indexOf(paramName + "="));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url + hash;
}

Call the function above in your onclick event.

How can I change correctly the parameters in the query string for a URL after the event handler onclick?

Try this -

<script>
function updateParams(abba){
if(abba.href.search('quantity-0') > -1) {
var a = abba.href.replace('quantity-0', 'add-0');
abba.setAttribute('href', a);
}
else {
var a = abba.href.replace('add-0','quantity-0');
abba.setAttribute('href',a);
}
}
</script>

Hope this helps. Thanks!

Change URL parameter onpage load

url = "http://example.com/A/B/Fixed_var_C";
url = url.substr(0,url.lastIndexOf('/'))+"/Dynamic_C";
alert(url);

Demo



Related Topics



Leave a reply



Submit