Gets a param from the search part of a URL by name - Node.js String

Node.js examples for String:URL String

Description

Gets a param from the search part of a URL by name

Demo Code


/**/*from w  ww . ja  va2s .  com*/
 * Gets a param from the search part of a URL by name.
 * @param {string} param URL parameter to look for.
 * @return {string|undefined} undefined if the URL parameter does not exist.
 */
function getURLParameter(param) {
  if (!window.location.search) {
    return;
  }
  var m = new RegExp(param + '=([^&]*)').exec(window.location.search.substring(1));
  if (!m) {
    return;
  }
  return decodeURIComponent(m[1]);
}

Related Tutorials