commit | author | age
|
19351a
|
1 |
/* $Id : utils.js 5052 2007-02-03 10:30:13Z weberliu $ */ |
B |
2 |
|
|
3 |
var Browser = new Object(); |
|
4 |
|
|
5 |
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined'); |
|
6 |
Browser.isIE = window.ActiveXObject ? true : false; |
|
7 |
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1); |
|
8 |
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1); |
|
9 |
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != - 1); |
|
10 |
|
|
11 |
var Utils = new Object(); |
|
12 |
|
|
13 |
Utils.htmlEncode = function(text) |
|
14 |
{ |
|
15 |
return text.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); |
|
16 |
} |
|
17 |
|
|
18 |
Utils.trim = function( text ) |
|
19 |
{ |
|
20 |
if (typeof(text) == "string") |
|
21 |
{ |
|
22 |
return text.replace(/^\s*|\s*$/g, ""); |
|
23 |
} |
|
24 |
else |
|
25 |
{ |
|
26 |
return text; |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
Utils.isEmpty = function( val ) |
|
31 |
{ |
|
32 |
switch (typeof(val)) |
|
33 |
{ |
|
34 |
case 'string': |
|
35 |
return Utils.trim(val).length == 0 ? true : false; |
|
36 |
break; |
|
37 |
case 'number': |
|
38 |
return val == 0; |
|
39 |
break; |
|
40 |
case 'object': |
|
41 |
return val == null; |
|
42 |
break; |
|
43 |
case 'array': |
|
44 |
return val.length == 0; |
|
45 |
break; |
|
46 |
default: |
|
47 |
return true; |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
Utils.isNumber = function(val) |
|
52 |
{ |
|
53 |
var reg = /^[\d|\.|,]+$/; |
|
54 |
return reg.test(val); |
|
55 |
} |
|
56 |
|
|
57 |
Utils.isInt = function(val) |
|
58 |
{ |
|
59 |
if (val == "") |
|
60 |
{ |
|
61 |
return false; |
|
62 |
} |
|
63 |
var reg = /\D+/; |
|
64 |
return !reg.test(val); |
|
65 |
} |
|
66 |
|
|
67 |
Utils.isEmail = function( email ) |
|
68 |
{ |
|
69 |
var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/; |
|
70 |
|
|
71 |
return reg1.test( email ); |
|
72 |
} |
|
73 |
|
|
74 |
Utils.isTel = function ( tel ) |
|
75 |
{ |
|
76 |
var reg = /^[\d|\-|\s|\_]+$/; //只允许使用数字-空格等 |
|
77 |
reg = /^(0[0-9]{2,3}-)?([0-9]{7,8})$/; |
|
78 |
|
|
79 |
return reg.test( tel ); |
|
80 |
} |
|
81 |
|
|
82 |
Utils.isMobile = function ( mobile ) |
|
83 |
{ |
|
84 |
var reg = /^1\d{10}$/; //11位数字,以1开头。 |
|
85 |
|
|
86 |
return reg.test( mobile ); |
|
87 |
} |
|
88 |
|
|
89 |
Utils.fixEvent = function(e) |
|
90 |
{ |
|
91 |
var evt = (typeof e == "undefined") ? window.event : e; |
|
92 |
return evt; |
|
93 |
} |
|
94 |
|
|
95 |
Utils.srcElement = function(e) |
|
96 |
{ |
|
97 |
if (typeof e == "undefined") e = window.event; |
|
98 |
var src = document.all ? e.srcElement : e.target; |
|
99 |
|
|
100 |
return src; |
|
101 |
} |
|
102 |
|
|
103 |
Utils.isTime = function(val) |
|
104 |
{ |
|
105 |
var reg = /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/; |
|
106 |
|
|
107 |
return reg.test(val); |
|
108 |
} |
|
109 |
|
|
110 |
Utils.x = function(e) |
|
111 |
{ //当前鼠标X坐标 |
|
112 |
return Browser.isIE?event.x + document.documentElement.scrollLeft - 2:e.pageX; |
|
113 |
} |
|
114 |
|
|
115 |
Utils.y = function(e) |
|
116 |
{ //当前鼠标Y坐标 |
|
117 |
return Browser.isIE?event.y + document.documentElement.scrollTop - 2:e.pageY; |
|
118 |
} |
|
119 |
|
|
120 |
Utils.request = function(url, item) |
|
121 |
{ |
|
122 |
var sValue=url.match(new RegExp("[\?\&]"+item+"=([^\&]*)(\&?)","i")); |
|
123 |
return sValue?sValue[1]:sValue; |
|
124 |
} |
|
125 |
|
|
126 |
Utils.$ = function(name) |
|
127 |
{ |
|
128 |
return document.getElementById(name); |
|
129 |
} |
|
130 |
|
|
131 |
function rowindex(tr) |
|
132 |
{ |
|
133 |
if (Browser.isIE) |
|
134 |
{ |
|
135 |
return tr.rowIndex; |
|
136 |
} |
|
137 |
else |
|
138 |
{ |
|
139 |
table = tr.parentNode.parentNode; |
|
140 |
for (i = 0; i < table.rows.length; i ++ ) |
|
141 |
{ |
|
142 |
if (table.rows[i] == tr) |
|
143 |
{ |
|
144 |
return i; |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
document.getCookie = function(sName) |
|
151 |
{ |
|
152 |
// cookies are separated by semicolons |
|
153 |
var aCookie = document.cookie.split("; "); |
|
154 |
for (var i=0; i < aCookie.length; i++) |
|
155 |
{ |
|
156 |
// a name/value pair (a crumb) is separated by an equal sign |
|
157 |
var aCrumb = aCookie[i].split("="); |
|
158 |
if (sName == aCrumb[0]) |
|
159 |
return decodeURIComponent(aCrumb[1]); |
|
160 |
} |
|
161 |
|
|
162 |
// a cookie with the requested name does not exist |
|
163 |
return null; |
|
164 |
} |
|
165 |
|
|
166 |
document.setCookie = function(sName, sValue, sExpires) |
|
167 |
{ |
|
168 |
var sCookie = sName + "=" + encodeURIComponent(sValue); |
|
169 |
if (sExpires != null) |
|
170 |
{ |
|
171 |
sCookie += "; expires=" + sExpires; |
|
172 |
} |
|
173 |
|
|
174 |
document.cookie = sCookie; |
|
175 |
} |
|
176 |
|
|
177 |
document.removeCookie = function(sName,sValue) |
|
178 |
{ |
|
179 |
document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;"; |
|
180 |
} |
|
181 |
|
|
182 |
function getPosition(o) |
|
183 |
{ |
|
184 |
var t = o.offsetTop; |
|
185 |
var l = o.offsetLeft; |
|
186 |
while(o = o.offsetParent) |
|
187 |
{ |
|
188 |
t += o.offsetTop; |
|
189 |
l += o.offsetLeft; |
|
190 |
} |
|
191 |
var pos = {top:t,left:l}; |
|
192 |
return pos; |
|
193 |
} |
|
194 |
|
|
195 |
function cleanWhitespace(element) |
|
196 |
{ |
|
197 |
var element = element; |
|
198 |
for (var i = 0; i < element.childNodes.length; i++) { |
|
199 |
var node = element.childNodes[i]; |
|
200 |
if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) |
|
201 |
element.removeChild(node); |
|
202 |
} |
|
203 |
} |