When are you supposed to use escape instead of encodeURI / encodeURIComponent?

前端 未结 15 1126
栀梦
栀梦 2020-11-21 07:39

When encoding a query string to be sent to a web server - when do you use escape() and when do you use encodeURI() or encodeURIComponent()

15条回答
  •  孤街浪徒
    2020-11-21 08:01

    The difference between encodeURI() and encodeURIComponent() are exactly 11 characters encoded by encodeURIComponent but not by encodeURI:

    Table with the ten differences between encodeURI and encodeURIComponent

    I generated this table easily with console.table in Google Chrome with this code:

    var arr = [];
    for(var i=0;i<256;i++) {
      var char=String.fromCharCode(i);
      if(encodeURI(char)!==encodeURIComponent(char)) {
        arr.push({
          character:char,
          encodeURI:encodeURI(char),
          encodeURIComponent:encodeURIComponent(char)
        });
      }
    }
    console.table(arr);

提交回复
热议问题