encodeURI, encodeURIComponent, decodeURI, decodeURIComponent 분석

Global (전역 역할 영역 에서 정 의 된 모든 속성 과 함 수 는 Global 의 속성) 대상 encodeURI(),encodeURIComponent() 방법 은 브 라 우 저 에 보 낼 수 있 도록 URI( ) 인 코딩 할 수 있 습 니 다.
유효한 URI 빈 칸 과 같은 문 자 를 포함 할 수 없습니다.이 2 개 URI 인 코딩 방법 은 URI 인 코딩 을 할 수 있 으 며, 특수 한 UTF 8 인 코딩 으로 모든 잘못된 문 자 를 대체 하여 브 라 우 저가 받 아들 일 수 있 도록 한다.
1. encodeURI(),encodeURIComponent()
먼저 demo 예 를 보십시오.
var uri = 'https://www.baidu.com/s?ie=utf-16&word=hello #index.html';

encodeURI(uri)      //https://www.baidu.com/s?ie=utf-16&word=hello%20#index.html

encodeURIComponent(uri) //https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-16%26word%3Dhello%20%23index.html

위의 예 를 통 해 알 수 있 듯 이 encodeURI() 인 코딩 후의 결 과 는 빈 칸 이 %20 으로 바 뀌 었 고 빈 칸 을 제외 한 모든 문자 가 바 뀌 지 않 았 다.encodeURIComponent() 는 모든 비 자모 숫자 문 자 를 대응 하 는 인 코딩 으로 바 꾸 는 것 이다.encodeURI() URI 전 체 를 사용 할 수 있 으 며 encodeURIComponent() 추가 URI 뒤의 문자열 에 만 적 용 됩 니 다.
그래서 일반적으로 우 리 는 전체 encodeURIComponent() 가 아 닌 검색 문자열 을 인 코딩 해 야 하기 때문에 URI 을 더 많이 사용 합 니 다.
2. decodeURI(),decodeURIComponent()
이 두 가지 방법 은 encodeURI(),encodeURIComponent() 과 대응 하 는데 그 중에서 decodeURI() 사용 encodeURI() 으로 교 체 된 문자 만 디 코딩 할 수 있 고 decodeURIComponent() 사용 encodeURIComponent() 으로 교 체 된 문 자 를 디 코딩 할 수 있다.
var uri = 'https://www.baidu.com/s?ie=utf-16&word=hello%20%24#index.html';

decodeURI(uri)      //https://www.baidu.com/s?ie=utf-16&word=hello %24#index.html

decodeURIComponent(uri) //https://www.baidu.com/s?ie=utf-16&word=hello $#index.html

uri 에 인 코딩 값 %20,%24 이 있 기 때문에 decodeURI%20 만 빈 칸 으로 바 꿀 수 있 고 %24 은 그 어떠한 처리 도 하지 않 습 니 다. %24 표시 $ 기호 가 사용 $ 으로 바 뀌 지 않 기 때 문 입 니 다.encodeURI 모든 특수 문자 의 인 코딩 을 설명 할 수 있다.
우 리 는 decodeURIComponent URL Search 의 매개 변 수 를 대상 으로 바 꾸 어 사용 할 수 있 습 니 다.
var str = 'https://www.sogou.com/sie?ie=utf8&query=%E5%91%B5%E5%91%B5&pid=AQKo5-0000';

var query = str.split('?')[1];

var result = {};

query.split("&").forEach(function(part) {
  var item = part.split("=");
  result[item[0]] = decodeURIComponent(item[1]);
});

console.log(result);

  :{ie: "utf8", query: "  ", pid: "AQKo5-0000"}

3. decodeURIComponent
ECMAScript v3 는 표준 에서 escape unescape 함 수 를 삭제 하고 사용 에 반대 하기 때문에 escape unescape 로 대체 해 야 합 니 다.decodeURI() decodeURIComponent() 방법 은 모든 유 니 코드 문 자 를 인 코딩 할 수 있 습 니 다. URI ASCLL 문자 만 정확하게 인 코딩 할 수 있 습 니 다. 아래 의 demo 를 볼 수 있 습 니 다. 인 코딩 결 과 는 약간 다 르 고 사용 하기 어렵 습 니 다.
var str = 'https://www.baidu.com/s?tn=mswin_oem_dg&ie=utf-16&word=hello';
escape(str)     //https%3A//www.baidu.com/s%3Ftn%3Dmswin_oem_dg%26ie%3Dutf-16%26word%3Dhello

좋은 웹페이지 즐겨찾기