Skip to content

Instantly share code, notes, and snippets.

@rose-pace
Last active June 18, 2018 15:05
Show Gist options
  • Select an option

  • Save rose-pace/8139751b94de446e7a19e7fb32e7c9d8 to your computer and use it in GitHub Desktop.

Select an option

Save rose-pace/8139751b94de446e7a19e7fb32e7c9d8 to your computer and use it in GitHub Desktop.

Revisions

  1. rose-pace revised this gist Nov 23, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UrlHelper.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ var UrlHelper = function (location) {
    var pairs = search.substr(1).split('&');
    for (var i = 0; i < pairs.length; i++) {
    var pair = pairs[i].split('=');
    this.queryValues[pair[0]] = pair[1];
    vals[pair[0]] = pair[1];
    }
    }

  2. rose-pace created this gist Nov 23, 2016.
    42 changes: 42 additions & 0 deletions UrlHelper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    var UrlHelper = function (location) {
    this.protocol = '';
    this.host = '';
    this.path = '';
    this.queryValues = {};

    this.initQueryValues = function (search) {
    var vals = {};

    if (search) {
    var pairs = search.substr(1).split('&');
    for (var i = 0; i < pairs.length; i++) {
    var pair = pairs[i].split('=');
    this.queryValues[pair[0]] = pair[1];
    }
    }

    return vals;
    };

    this.getSearchString = function () {
    var search = "?";
    if (this.queryValues) {
    for (var key in this.queryValues) {
    search += key + "=" + this.queryValues[key] + "&";
    }
    }
    return search.substr(0, search.length - 1); //drop trailing &
    };

    this.toString = function () {
    return this.protocol + "//" + this.host + this.path + this.getSearchString();
    };

    //init
    if (location) {
    this.protocol = location.protocol;
    this.host = location.host;
    this.path = location.pathname;
    this.queryValues = this.initQueryValues(location.search);
    }
    }