Asked 7 years ago
14 Jan 2017
Views 1088
sachin

sachin posted

how to encode in JavaScript ?

how to encode in JavaScript ?
how to do base64encode , encode , url encode in JavaScript ?
Rasi

Rasi
answered Nov 30 '-1 00:00

encodeURI is Native JavaScript function which encode url .


encodeURI("http://arrayoverflow.com/question/break-nested-loop-in-javascript/271");
//http://arrayoverflow.com/question/break-nested-loop-in-javascript/271



more specific encodeURI

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}



encodeURIComponent also encode Url


encodeURIComponent("http://arrayoverflow.com/question/break-nested-loop-in-javascript/271");
//"http%3A%2F%2Farrayoverflow.com%2Fquestion%2Fbreak-nested-loop-in-javascript%2F271"


more specific encodeURIComponent

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}


Post Answer