-
Notifications
You must be signed in to change notification settings - Fork 114
/
urlencode.html
177 lines (174 loc) · 5.96 KB
/
urlencode.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
</head>
<body>
<input type="text" id="input" value="中文">
<input type="button" id="btn" value="urlencode">
<select id="charset">
<option value="gbk">gbk</option>
<option value="big5">big5</option>
<option value="utf-8">utf-8</option>
</select>
<div>
编码结果:
<textarea style="display: block" id="output"></textarea>
</div>
<div>
解码: <input type="button" id="deBtn" value="urldecode">
<textarea style="display: block" id="outputDecode"></textarea>
</div>
<!--<img src="data:text/javascript;charset=gbk,window.a='%D6%D0%CE%C4'" id="d">-->
<!--<script charset="gbk" src="data:text/javascript;charset=gbk,alert('%CE%C4')"></script>-->
<script>
/*
var s=document.createElement('script');
s.charset='gbk';
var src=document.getElementById('d').src;
alert(src);
s.src=src;
document.body.appendChild(s);
setTimeout(function(){
alert(window.a)
},300)
*/
function urlencode(str, charset, callback) {
//创建form通过accept-charset做encode
var form = document.createElement('form');
form.method = 'get';
form.style.display = 'none';
form.acceptCharset = charset;
if (document.all) {
//如果是IE那么就调用document.charset方法
window.oldCharset = document.charset;
document.charset = charset;
}
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'str';
input.value = str;
form.appendChild(input);
form.target = '_urlEncode_iframe_';
document.body.appendChild(form);
//隐藏iframe截获提交的字符串
if (!window['_urlEncode_iframe_']) {
var iframe;
if(document.all){
try{
iframe = document.createElement('<iframe name="_urlEncode_iframe_"></iframe>');
}catch(e){
iframe = document.createElement('iframe');
//iframe.name = '_urlEncode_iframe_';
iframe.setAttribute('name', '_urlEncode_iframe_');
}
}else{
iframe = document.createElement('iframe');
//iframe.name = '_urlEncode_iframe_';
iframe.setAttribute('name', '_urlEncode_iframe_');
}
iframe.style.display = 'none';
iframe.width = "0";
iframe.height = "0";
iframe.scrolling = "no";
iframe.allowtransparency = "true";
iframe.frameborder = "0";
iframe.src = 'about:blank';
document.body.appendChild(iframe);
}
//
window._urlEncode_iframe_callback = function(str) {
callback(str);
if (document.all) {
document.charset = window.oldCharset;
}
}
//设置回调编码页面的地址,这里需要用户修改
form.action = 'getEncodeStr.html';
form.submit();
setTimeout(function() {
form.parentNode.removeChild(form);
iframe.parentNode.removeChild(iframe);
}, 500)
}
function urldecode(str, charset, callback) {
var script = document.createElement('script');
script.id = '_urlDecodeFn_';
window._urlDecodeFn_ = callback;
if (document.all) {
if(navigator.userAgent.indexOf('MSIE 8')>-1){
//对于ie8做特别hack
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.charset = charset;
link.href = 'data:text/plain;charset=' + charset + ',%23_decode_hidden_el_for_test_%7Bbackground-image%3Aurl(' + str + ')%7D';
alert(link.href);
document.body.appendChild(link);
//然后创建一个隐藏的div,应用这个样式
var div = document.createElement('div');
div.id = '_decode_hidden_el_for_test_';
div.style.display = 'none';
document.body.appendChild(div);
setTimeout(function(){
callback(document.getElementById('_decode_hidden_el_for_test_').currentStyle['backgroundImage'].match(/\("data\:text\/(.+)"\)/)[1]);
link.parentNode.removeChild(link);
div.parentNode.removeChild(div);
}, 300)
}else{
//隐藏iframe截获提交的字符串
if (!window['_urlDecode_iframe_']) {
var iframe;
if(document.all){
try{
iframe = document.createElement('<iframe name="_urlDecode_iframe_"></iframe>');
}catch(e){
iframe = document.createElement('iframe');
//iframe.name = '_urlDecode_iframe_';
iframe.setAttribute('name', '_urlDecode_iframe_');
}
}else{
iframe = document.createElement('iframe');
//iframe.name = '_urlDecode_iframe_';
iframe.setAttribute('name', '_urlDecode_iframe_');
}
//iframe.name = '_urlDecode_iframe_';
iframe.setAttribute('name', '_urlDecode_iframe_');
iframe.style.display = 'none';
iframe.width = "0";
iframe.height = "0";
iframe.scrolling = "no";
iframe.allowtransparency = "true";
iframe.frameborder = "0";
iframe.src = 'about:blank';
document.body.appendChild(iframe);
}
//ie下需要指明charset,然后src=datauri才可以
iframe.contentWindow.document.write('<html><scrip' + 't charset="gbk" src="data:text/javascript;charset=gbk,parent._decodeStr_=\'' + str + '\'"></s'+'cript></html>');
setTimeout(function() {
callback(_decodeStr_);
iframe.parentNode.removeChild(iframe);
}, 300)
}
} else {
var src = 'data:text/javascript;charset=' + charset + ',_urlDecodeFn_("' + str + '");';
src += 'document.getElementById("_urlDecodeFn_").parentNode.removeChild(document.getElementById("_urlDecodeFn_"));';
script.src = src;
document.body.appendChild(script);
}
}
/**编码和解码
*/
document.getElementById('btn').onclick = function() {
urlencode(document.getElementById('input').value, document.getElementById('charset').value, function(str) {
document.getElementById('output').value = str;
})
}
document.getElementById('deBtn').onclick = function() {
urldecode(document.getElementById('output').value, document.getElementById('charset').value, function(str) {
document.getElementById('outputDecode').value = str;
})
}
</script>
</body>
</html>