-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
296 lines (255 loc) · 7.24 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>短网址生成服务</title>
<style>
*{margin:0;padding:0;}
textarea{resize:none;}
input,input:focus,textarea,textarea:focus{outline:none;border:none;}
body{font-size:16px;font-family:"Microsoft Yahei","宋体",arial;}
.bd-wrap{width:720px;margin:30px auto;}
.placeholder-box{position:relative;}
.placeholder{position:absolute;left:0;top:0;line-height:22px;padding:6px;font-size:12px;color:#777;text-indent:10px;}
.placeholder:focus{}
.add-url{border:1px solid #8cc665;line-height:22px;padding:6px;width:706px;text-indent:10px;}
.add-url:focus{border:1px solid #44a340;}
.shorten{
line-height:24px;
padding:6px 12px;
display:inline-block;
color:#fff;
background:#44a340;
cursor:pointer;
font-size:16px;
vertical-align:top;
}
.shorten-box{
text-align:right;
}
</style>
</head>
<body>
<div id="hd">
<div id="logo-box"></div>
</div>
<div id="bd" class="bd-wrap">
<div id="url-box" class="url-box">
<h1>网址缩短</h1>
<div id="placeholder-box" class="placeholder-box">
<textarea name="addurl" id="J-add-url" class="add-url" cols="30" rows="10"></textarea>
<span class="placeholder" id="J-placeholder">请输入http://或https://开头的网址</span>
</div>
<div id="shorten-box" class="shorten-box">
<input type="submit" class="shorten" id="J-shorten" value="生成网址">
</div>
</div>
</div>
<script>
(function(doc,att){
var _attax=function $(selector){
var aChild=[];
switch(selector.charAt(0)){
case '#': //id
var aEle=document.getElementById(selector.substring(1));
aChild.push(aEle);
break;
case '.': //class
var aEle=getElementsByClassName(document,selector.substring(1));
for(var j=0;j<aEle.length;j++){
aChild.push(aEle[j]);
}
break;
}
return aChild;
};
function getElementsByClassName(parentNode,sClass){
if(document.getElementsByClassName){
return parentNode.getElementsByClassName(sClass);
}else{
//获取所有标签
var aEle=document.getElementsByTagName('*');
//空数组存储获取到的className元素
var result=[];
//匹配sClass的正则
var regExp=new RegExp('\\b'+sClass+'\\b');
for(var i=0;i<aEle.length;i++){
if(regExp.test(aEle[i].className)){
result.push(aEle[i]);
}
}
//循环结束后,返回所有className的元素
return result;
}
};
(function(attax){
//判断DOMReady方法是否执行过
var isReady=false;
//存储需要执行的函数
var readyList=[];
var timer=null;
attax.ready=function(fn){
if(isReady){
fn.call(document);
}else{
readyList.push(
function(){
return fn.call(this);
}
);
}
return this;
}
var onDOMReady=function(){
for(var i=0;i<readyList.length;i++){
readyList[i].apply(document);
}
readyList=null;
}
var bindReady=function(){
if(isReady) return;
isReady=true;
onDOMReady.call(window);
if(document.removeEventListener){
document.removeEventListener('DOMContentLoaded',bindReady,false);
} else if(document.attachEvent){
document.detachEvent('onreadystatechange',bindReady);
if(window==window.top){
clearInterval(timer);
timer=null;
}
}
};
if(document.addEventListener){
document.addEventListener('DOMContentLoaded',bindReady,false);
}
else if(document.attachEvent){
document.attachEvent('onreadystatechange',function(){
if((/loaded|complete/).test(document.readyState)){
bindReady();
}
});
if(window==window.top){
timer=setInterval(function(){
try{
//在IE下面用doScroll能否执行来判断DOM是否加载完成
isReady||document.documentElement.doScroll('left');
}catch(e){
return;
}
bindReady();
},1)
}
}
})(_attax)
_attax.prototype={
css:function(){
}
};
//ajax、表单之类的
function json2url(json) {
var a = [];
for (var i in json) {
var v = json[i] + '';
v = v.replace(/\n/g, '<br/>');
v = encodeURIComponent(v);
a.push(i + '=' + v);
}
return a.join('&');
}
_attax.ajax=function ajax(url, opt) {
opt = opt || {};
opt.data = opt.data || {};
opt.data.t = opt.data.t || new Date().getTime();
opt.method = opt.method || 'GET';
var oAjax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
if (opt.method == 'POST') {
oAjax.open('POST', url, true);
oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
try {
oAjax.send(opt.data ? json2url(opt.data) : null);
} catch (e) {}
} else {
url += '?' + json2url(opt.data);
oAjax.open('GET', url, true);
try {
oAjax.send();
} catch (e) {}
}
oAjax.onreadystatechange = function() {
if (oAjax.readyState == 4) {
if (oAjax.status == 200) {
opt.success && opt.success(oAjax.responseText);
} else {
opt.failed && opt.failed(oAjax.status);
}
}
};
};
_attax.trim=function(str){
return str.replace(/(^\s*)|(\s*$)/g,'');
};
_attax.ltrim=function(str){
return str.replace(/(^\s*)/g,'');
};
_attax.ltrim=function(str){
return str.replace(/(\s*$)/g,'');
}
window.$=_attax;
})(document,'attax');
$.ready(function(){
var placeholder=$('#J-placeholder')[0];
var url=$('#J-add-url')[0];
placeholder.onclick=url.onfocus=function(){
placeholder.style.display='none';
};
url.onblur=function(){
var urlVal=url.value;
console.log(urlVal);
if(urlVal==''){
placeholder.style.display='block';
}else{
placeholder.style.display='none';
}
};
$('#J-shorten')[0].onclick=function(){
//
var val=$('#J-add-url')[0].value;
var _val=$.trim($('#J-add-url')[0].value);
console.log(_val)
//空白
var whiteReg=/^\s+$/;
//网址正则
var strRegex="^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var urlRegex=new RegExp(strRegex);
if(whiteReg.test(val)){
alert('请输入网址')
return false;
}
if(!urlRegex.test(_val)){
alert('抱歉,您输入的网址不符合规范,请检查后重新输入!');
}else{
console.log(_val)
$.ajax('/addurl',{
data:{
url:_val
},
success:function(data){
alert(data);
}
});
}
}
});
</script>
</body>
</html>