Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Last active April 13, 2019 02:54
Show Gist options
  • Save a1exlism/dc41f8c5d5940123bc75eb7535de2881 to your computer and use it in GitHub Desktop.
Save a1exlism/dc41f8c5d5940123bc75eb7535de2881 to your computer and use it in GitHub Desktop.

Purpose

For transfering data.

When to use

As the name self says.

  • Only parameter to encode

encodeURI

  • Whole URI as para-meter

encodeURIComponent

Example

let uri1 = 'https://www.google.com?result_1=10%9&&result_2=4*6';
let uri2 = 'https://www.google.com/redict2/';
let newUri = uri2 + encodeURIComponent(uri1);

console.log(encodeURI(uri1));
//  https://www.google.com?result_1=10%259&&result_2=4*6

console.log(uri1+'\n'+uri2+'\n'+newUri);

/*
  https://www.google.com?result_1=10%9&&result_2=4*6
  https://www.google.com/redict2/
  https://www.google.com/redict2/https%3A%2F%2Fwww.google.com%3Fresult_1%3D10%259%26%26result_2%3D4*6
*/

Refer

简单明了区分escape、encodeURI和encodeURIComponent | CNblogs difference between escape, encodeuri, encodeURIComponent | Stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment