웹 페이지에서 올바르게 볼 수 있도록 URL 문자열 (HTTP 주소)의 HREF 매개 변수를 인코딩/디코딩 (변환)하는 간단한 jQuery 코드 스 니펫. 예를 들어, HTML은 공간과 동등하고 @는 암페어 ( @)입니다.
<span>var url = $(location).attr('href'); //get current url </span><span>//OR </span><span>var url = 'folder/index.html?param=#23dd&noob=yes'; //or specify one </span> <span>var encodedUrl = encodeURIComponent(url); </span><span>console.log(encodedUrl); </span><span>//outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes</span>
<span>var url = $(location).attr('href'); //get current url </span><span>//OR </span><span>var url = 'folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes'; //or specify one </span> <span>var decodedUrl = decodeURIComponent(url); </span><span>console.log(decodedUrl); </span><span>//outputs folder/index.html?param=#23dd&noob=yes</span>jQuery에서 URL을 디코딩하려면 jQuery에서 URL을 디코딩하려면 DecodeUricomponent () 기능을 사용할 수 있습니다. 이 함수는 URL의 인코딩 된 구성 요소를 디코딩하여 원래 문자열을 반환 할 수 있습니다. 간단한 예는 다음과 같습니다.
space %20 ! %21 " %22 # %23 $ %24 % %25 & %26 ' %27 ( %28 ) %29 * %2A + %2B , %2C - %2D . %2E / %2F ... etcjQuery를 사용하여 URL 매개 변수와 함께 ajax 요청을 보내는 방법?
인코딩 된 URL이 올바르게 작동하지 않으면 몇 가지 이유로 인해 발생할 수 있습니다. 일반적인 문제 중 하나는 URL 매개 변수를 올바르게 인코딩하지 않습니다. 또 다른 문제는 개별 URI 구성 요소에 encodeUricomponent () 대신 encodeuri ()를 사용하는 것일 수 있습니다. 항상 특정 요구에 맞는 기능을 사용하고 있는지 확인하십시오.
DecodeUricomponent 함수는 jQuery에서 무엇을 하는가? decodeUricomponent () 함수는 jquery에서 사용할 수있는 내장 Javascript 함수입니다. 이전에 EncodeUricomponent ()에 의해 생성 된 URI 구성 요소를 유사한 루틴으로 디코딩합니다. 디코딩 된 URI를 나타내는 문자열을 반환합니다.
위 내용은 jQuery encode/decode url string의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!