Skip to content

Instantly share code, notes, and snippets.

@superalsrk
Created January 23, 2017 16:20
Show Gist options
  • Select an option

  • Save superalsrk/0effb79f2aeb559b36b30fdfeb062b71 to your computer and use it in GitHub Desktop.

Select an option

Save superalsrk/0effb79f2aeb559b36b30fdfeb062b71 to your computer and use it in GitHub Desktop.

Revisions

  1. superalsrk created this gist Jan 23, 2017.
    29 changes: 29 additions & 0 deletions update_parameter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    function UpdateQueryString(key, value, url) {
    if (!url) url = window.location.href;
    var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
    hash;

    if (re.test(url)) {
    if (typeof value !== 'undefined' && value !== null)
    return url.replace(re, '$1' + key + "=" + value + '$2$3');
    else {
    hash = url.split('#');
    url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
    if (typeof hash[1] !== 'undefined' && hash[1] !== null)
    url += '#' + hash[1];
    return url;
    }
    }
    else {
    if (typeof value !== 'undefined' && value !== null) {
    var separator = url.indexOf('?') !== -1 ? '&' : '?';
    hash = url.split('#');
    url = hash[0] + separator + key + '=' + value;
    if (typeof hash[1] !== 'undefined' && hash[1] !== null)
    url += '#' + hash[1];
    return url;
    }
    else
    return url;
    }
    }