forked from Chuyue0/javascript-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_encodeStr.html
28 lines (23 loc) · 1.29 KB
/
js_encodeStr.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS编码解码了解</title>
</head>
<body>
<script type="text/javascript">
var str='我是谁?Who are you?',str2='https://www.gude.com?%id=1#type=1';
str=encodeURI(str);console.log("中文编码:"+str);//"中文编码:%E6%88%91%E6%98%AF%E8%B0%81%EF%BC%9FWho%20are%20you?"
str=decodeURI(str);console.log("中文解码:"+str);//"中文解码:我是谁?Who are you?"
str2=encodeURI(str2);console.log("网址编码:"+str2);//"网址编码:https://www.gude.com?%25id=1#type=1"
str2=decodeURI(str2);console.log("网址编码:"+str2);//"网址编码:https://www.gude.com?%id=1#type=1"
//对比str2
var comStr='https://www.gude.com?%id=1#type=1';
comStr=encodeURIComponent(comStr);console.log("组成部分编码:"+comStr);//"组成部分编码:https%3A%2F%2Fwww.gude.com%3F%25id%3D1%23type%3D1"
comStr=decodeURIComponent(comStr);console.log("组成部分解码:"+comStr);//"组成部分解码:https://www.gude.com?%id=1#type=1"
//对比
comStr=escape(comStr);console.log("字符串编码:"+comStr);//"字符串编码:https%3A//www.gude.com%3F%25id%3D1%23type%3D1"
comStr=unescape(comStr);console.log("字符串解码:"+comStr);//"字符串解码:https://www.gude.com?%id=1#type=1"
</script>
</body>
</html>