Home > Web Front-end > JS Tutorial > body text

Urlencode encoding and decoding method under Javascript with decodeURIComponent_javascript tips

WBOY
Release: 2016-05-16 18:29:00
Original
1017 people have browsed it

Regarding the encoding results of ASP (Server.UrlEncode), PHP (urlencode()) functions, or the Chinese characters written directly into COOKIES through dynamic languages ​​such as asp and php, when reading them with JS, you will encounter an encoding problem. , that is, the final string is encoded by urlencode, and sometimes it is necessary to read the data from JS on the client side.

This article will briefly talk about how to solve this problem in js through the system’s own functions.

I believe that friends who have encountered this problem should have some understanding. Currently, some custom functions under js are popular on the Internet to solve this problem, such as vbscript (URLDecode()), javascript (UrlDecode( ))wait. Neither of these two functions can communicate well with each other asp (Server.UrlEncode) and php (urlencode()).
About vbscript (function URLDecode()) and javascript (function UrlDecode()) will also be reproduced at the end of this article.

The protagonist of this article is javascript (UrlDecodedecodeURIComponent()). This function name is so common that I don’t really understand it personally. After all, there are many system functions in js and it is easy to miss it. Worry discovered this function by accident!

Encoding function: encodeURIComponent()
Decoding function: decodeURIComponent()
decodeURIComponent() Syntax

Copy code The code is as follows:

decodeURIComponent(URIstring)
Parameter: (URIstring) required. A string containing the encoded URI components or other text to be decoded.
Return value: A copy of the URIstring in which the hexadecimal escape sequences are replaced by the characters they represent.

Example:
Copy code The code is as follows:

< ;script type="text/javascript">
var test1="Trouble";
var test2="Trouble";
document.write("Encoding (original=" test1 "):" encodeURIComponent (test1) "
");
document.write("Decode (original =" test2 "): " decodeURIComponent(test2));



Result:
Copy code The code is as follows:

Encoding (original =Trouble):Trouble
Decoding (original=Trouble):Trouble


Note: This article is only tested in UTF-8 encoding environment. Because in different encoding environments, the compiled code of asp (Server.UrlEncode) seems to be different and needs to be tested!
Attached reprint:
vbscript (function URLDecode())
Copy code The code is as follows:



javascript(function UrlDecode())其实还是柔和使用了vbscript,好像在javascript环境中,对于asc、hex、chr相关的转换,如 str.charCodeAt(0).toString(16) 及 String.fromCharCode(str) 在不同编码下,对于中文的编码结果还不统一。
比如: vbscript str2asc/asc2str
复制代码 代码如下:




javascript str2asc/asc2str
复制代码 代码如下:




演示:

[Ctrl A 全选 注:
如需引入外部Js需刷新才能执行]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template