Skip to content

Instantly share code, notes, and snippets.

@excalq
Last active January 5, 2025 10:33
Show Gist options
  • Save excalq/2961415 to your computer and use it in GitHub Desktop.
Save excalq/2961415 to your computer and use it in GitHub Desktop.

Revisions

  1. excalq revised this gist Mar 19, 2024. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    // 2024 Update, use URLSearchParams [https://caniuse.com/urlsearchparams]
    export function createQueryString2(name: string, value: string, searchParams: any) {
    const params = new URLSearchParams(searchParams);
    params.set(name, value.toLowerCase());

    return params.toString();
    }

    // ---- Original 2012 version, when browsers really sucked ----
    // Explicitly save/update a url parameter using HTML5's replaceState().
    function updateQueryStringParam(param, value) {
    baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
  2. excalq revised this gist Jun 20, 2012. 1 changed file with 17 additions and 18 deletions.
    35 changes: 17 additions & 18 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,19 @@
    // Explicitly save/update a url parameter using HTML5's replaceState().
    // Works for HTML5 supporting browsers.
    function updateQueryStringParam(param, value) {
    baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
    urlQueryString = document.location.search;
    var newParam = key + '=' + value,
    params = '?' + newParam;
    // Explicitly save/update a url parameter using HTML5's replaceState().
    function updateQueryStringParam(param, value) {
    baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
    urlQueryString = document.location.search;
    var 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;
    }
    // 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);
    }
    }
    window.history.replaceState({}, "", baseUrl + params);
    }
  3. excalq created this gist Jun 20, 2012.
    20 changes: 20 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // Explicitly save/update a url parameter using HTML5's replaceState().
    // Works for HTML5 supporting browsers.
    function updateQueryStringParam(param, value) {
    baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
    urlQueryString = document.location.search;
    var 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);
    }