Add URL to page and refresh page

I want to write a part of javascript that will add a parameter to the current URL and then refresh the page - how can I do this?

+111
javascript jquery refresh
May 13 '11 at 20:33
source share
8 answers

this should work (not verified!)

var url = window.location.href; if (url.indexOf('?') > -1){ url += '&param=1' }else{ url += '?param=1' } window.location.href = url; 
+143
May 13 '11 at 20:36
source share

Shorter than the accepted answer, doing the same thing, but keeping it simple:

 window.location.search += '&param=42'; 

We do not need to change the entire URL, just the query string, known as the location search attribute.

When you assign a value to a search attribute, the question mark is automatically inserted by the browser and the page reloads.

+118
May 13 '11 at 9:58 p.m.
source share

Most of the answers here suggest adding a parameter to the URL, something like the following snippet or similar:

 location.href = location.href + "&parameter=" + value; 

This will work well for most cases.

However

This is not the right way to add a parameter to the url in my opinion.

Since the proposed approach does not check if the parameter is set in the URL, if it is not careful, you may have a very long URL with the same parameter that is repeated several times. i.e:

 https://stackoverflow.com/?&param=1&param=1&param=1&param=1&param=1&param=1&param=1&param=1&param=1 

at this point there are problems. The proposed approach can and will create a very long URL after updating several pages, which makes the URL invalid. Follow this link for more information on long URLs. What is the maximum length of a URL in different browsers?

This is my suggested approach:

 function URL_add_parameter(url, param, value){ var hash = {}; var parser = document.createElement('a'); parser.href = url; var parameters = parser.search.split(/\?|&/); for(var i=0; i < parameters.length; i++) { if(!parameters[i]) continue; var ary = parameters[i].split('='); hash[ary[0]] = ary[1]; } hash[param] = value; var list = []; Object.keys(hash).forEach(function (key) { list.push(key + '=' + hash[key]); }); parser.search = '?' + list.join('&'); return parser.href; } 

Using this function, you will need to do the following:

 location.href = URL_add_parameter(location.href, 'param', 'value'); 
+61
Jan 15 '15 at 18:59
source share
 location.href = location.href + "&parameter=" + value; 
+2
May 13 '11 at 20:36
source share
 function gotoItem( item ){ var url = window.location.href; var separator = (url.indexOf('?') > -1) ? "&" : "?"; var qs = "item=" + encodeURIComponent(item); window.location.href = url + separator + qs; } 

Additional Compatibility Version

 function gotoItem( item ){ var url = window.location.href; url += (url.indexOf('?') > -1)?"&":"?" + "item=" + encodeURIComponent(item); window.location.href = url; } 
+2
May 13 '11 at 20:45
source share

Please check the code below:

 /*Get current URL*/ var _url = location.href; /*Check if the url already contains ?, if yes append the parameter, else add the parameter*/ _url = ( _url.indexOf('?') !== -1 ) ? _url+'&param='+value : _url+'?param='+value; /*reload the page */ window.location.href = _url; 
0
Nov 12 '18 at 9:27
source share

One small fix for @yeyo's thoughtful answer above.

Edit:

var parameters = parser.search.split(/\?|&/);

In order to:

var parameters = parser.search.split(/\?|&amp;/);

0
Apr 04 '19 at 18:40
source share

Try this

 var url = ApiUrl('/customers'); if(data){ url += '?search='+data; } else { url += '?page=${page}&per_page=${perpage}'; } console.log(url); 
0
Jun 11 '19 at 5:55
source share



All Articles