Design & Download
Your JS Library
in Seconds!!!

Snippet: Encoding & Decoding URI Parameters

Written by Christopher West (cwest) on November 15, 2015.
// The same as PHP's (from php.js) urlencode().
function escape(str) {
  return encodeURIComponent(str)
    .replace(/!/g, '%21')
    .replace(/'/g, '%27')
    .replace(/\(/g, '%28')
    .replace(/\)/g, '%29')
    .replace(/\*/g, '%2A')
    .replace(/%20/g, '+');
}

// The same as PHP's (from php.js) urldecode().
function unescape(str) {
  return decodeURIComponent(str.replace(/%(?![\dA-F]{2})/gi, '%25').replace(/\+/g, '%20'));
}