utils/uri.js
function updateParamsFromUrl(config) {
  // If there is no query string in the URL, then there's nothing to do in this
  // function, so break early.
  if (!location.search) {
    return;
  }

  var c = objects.clone(UpdateParamsFromUrlDefaultConfig);
  objects.merge(c, config);
  var selector = c.selector;
  var attr = c.attr;
  var params = c.params;
  if (!params) {
    throw '`params` is required';
  }

  var vals = {};
  parseQueryString(location.search, function(key, value) {
    for (var i = 0; i < params.length; i++) {
      var param = params[i];
      if (param instanceof RegExp) {
        if (key.match(param)) {
          vals[key] = value;
        }
      } else {
        if (param === key) {
          vals[key] = value;
        }
      }
    }
  });

  var els = document.querySelectorAll(selector);
  for (var i = 0, el; el = els[i]; i++) {
    var href = el.getAttribute(attr);
    if (href && !href.startsWith('#')) {
      var url = new URL(el.getAttribute(attr), location.href);
      var map = parseQueryMap(url.search);
      // Optionally serialize the keys and values into a single key and value,
      // and rewrite the element's attribute with the new serialized query
      // string. This was built specifically to do things like pass `utm_*`
      // parameters into a `referrer` query string, for Google Play links, e.g.
      // `&referrer=utm_source%3DSOURCE`.
      // See https://developers.google.com/analytics/devguides/collection/android/v4/campaigns#google-play-url-builder
      var serializeIntoKey = el.getAttribute(c.serializeAttr);
      if (serializeIntoKey) {
        var serializedQueryString = encodeQueryMap(vals);
        map[serializeIntoKey] = serializedQueryString;
      } else {
        for (var key in vals) {
          map[key] = vals[key];
        }
      }
      url.search = encodeQueryMap(map);
      el.setAttribute(attr, url.toString());
    }
  }
}
function updateParamsFromUrl(config) {
  // If there is no query string in the URL, then there's nothing to do in this
  // function, so break early.
  if (!location.search) {
    return;
  }

  var c = objects.clone(UpdateParamsFromUrlDefaultConfig);
  objects.merge(c, config);
  var selector = c.selector;
  var attr = c.attr;
  var params = c.params;
  if (!params) {
    throw '`params` is required';
  }

  var vals = {};
  parseQueryString(location.search, function(key, value) {
    for (var i = 0; i < params.length; i++) {
      var param = params[i];
      if (param instanceof RegExp) {
        if (key.match(param)) {
          vals[key] = value;
        }
      } else {
        if (param === key) {
          vals[key] = value;
        }
      }
    }
  });

  var els = document.querySelectorAll(selector);
  for (var i = 0, el; el = els[i]; i++) {
    var href = el.getAttribute(attr);
    if (href && !href.startsWith('#')) {
      var url = new URL(el.getAttribute(attr), location.href);
      var map = parseQueryMap(url.search);
      // Optionally serialize the keys and values into a single key and value,
      // and rewrite the element's attribute with the new serialized query
      // string. This was built specifically to do things like pass `utm_*`
      // parameters into a `referrer` query string, for Google Play links, e.g.
      // `&referrer=utm_source%3DSOURCE`.
      // See https://developers.google.com/analytics/devguides/collection/android/v4/campaigns#google-play-url-builder
      var serializeIntoKey = el.getAttribute(c.serializeAttr);
      if (serializeIntoKey) {
        var serializedQueryString = encodeQueryMap(vals);
        map[serializeIntoKey] = serializedQueryString;
      } else {
        for (var key in vals) {
          map[key] = vals[key];
        }
      }
      url.search = encodeQueryMap(map);
      el.setAttribute(attr, url.toString());
    }
  }
}
function updateParamsFromUrl ( config ) { // If there is no query string in the URL, then there's nothing to do in this // function, so break early. if ( ! location . search ) { return ; } var c = objects . clone ( UpdateParamsFromUrlDefaultConfig ) ; objects . merge ( c , config ) ; var selector = c . selector ; var attr = c . attr ; var params = c . params ; if ( ! params ) { throw '`params` is required' ; } var vals = { } ; parseQueryString ( location . search , function ( key , value ) { for ( var i = 0 ; i < params . length ; i ++ ) { var param = params [ i ] ; if ( param instanceof RegExp ) { if ( key . match ( param ) ) { vals [ key ] = value ; } } else { if ( param === key ) { vals [ key ] = value ; } } } } ) ; var els = document . querySelectorAll ( selector ) ; for ( var i = 0 , el ; el = els [ i ] ; i ++ ) { var href = el . getAttribute ( attr ) ; if ( href && ! href . startsWith ( '#' ) ) { var url = new URL ( el . getAttribute ( attr ) , location . href ) ; var map = parseQueryMap ( url . search ) ; // Optionally serialize the keys and values into a single key and value, // and rewrite the element's attribute with the new serialized query // string. This was built specifically to do things like pass `utm_*` // parameters into a `referrer` query string, for Google Play links, e.g. // `&referrer=utm_source%3DSOURCE`. // See https://developers.google.com/analytics/devguides/collection/android/v4/campaigns#google-play-url-builder var serializeIntoKey = el . getAttribute ( c . serializeAttr ) ; if ( serializeIntoKey ) { var serializedQueryString = encodeQueryMap ( vals ) ; map [ serializeIntoKey ] = serializedQueryString ; } else { for ( var key in vals ) { map [ key ] = vals [ key ] ; } } url . search = encodeQueryMap ( map ) ; el . setAttribute ( attr , url . toString ( ) ) ; } } }
Updates the URL attribute of elements with query params from the current URL.
@param {Object} config Config object. Properties are:
selector: CSS query selector of elements to act upon.
attr: The element attribute to update.
params: A list of URL params (string or RegExp) to set on the element
attr from the current URL.
Updates the URL attribute of elements with query params from the current URL .