Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a way of accomplishing this. It takes the param name and param value, and an optional 'clear'. If you supply clear=true, it will remove all other params and just leave the newly added one - in other cases, it will either replace the original with the new, or add it if it's not present in the querystring.</p> <p>This is modified from the original top answer as that one broke if it replaced anything but the last value. This will work for any value, and preserve the existing order.</p> <pre><code>function setGetParameter(paramName, paramValue, clear) { clear = typeof clear !== 'undefined' ? clear : false; var url = window.location.href; var queryString = location.search.substring(1); var newQueryString = ""; if (clear) { newQueryString = paramName + "=" + paramValue; } else if (url.indexOf(paramName + "=") &gt;= 0) { var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }; var keyValues = queryString.split('&amp;'); for(var i in keyValues) { var key = keyValues[i].split('='); if (key.length &gt; 1) { if(newQueryString.length &gt; 0) {newQueryString += "&amp;";} if(decode(key[0]) == paramName) { newQueryString += key[0] + "=" + encodeURIComponent(paramValue);; } else { newQueryString += key[0] + "=" + key[1]; } } } } else { if (url.indexOf("?") &lt; 0) newQueryString = "?" + paramName + "=" + paramValue; else newQueryString = queryString + "&amp;" + paramName + "=" + paramValue; } window.location.href = window.location.href.split('?')[0] + "?" + newQueryString; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload