具体代码如下:
function
fLoginFormSubmit()
{
var
fm
=
window.document.login163;
var
user
=
fm.username;
user.value
=
fTrim( user.value);
//
Trim the input value.
if
( user.value
==
""
) {
window.alert(
"
\请输入您的用户名 ?
"
);
user.focus();
event.returnValue
=
false
;
return
false
;
}
if
( fm.password.value.length
==
""
) {
window.alert(
"
\请输入您的密码 ?
"
);
fm.password.focus();
event.returnValue
=
false
;
return
false
;
}
var
ati
=
user.value.indexOf(
"
@
"
);
if
( ati
!=
-
1
){
user.value
=
user.value.substring(
0
, ati);
}
var
secure
=
fm.remUser.checked
?
true
:
false
;
//
var url = "http://reg.163.com/in.jsp?url=http://mimg.163.com/oy_vip.htm?";
var
url
=
"
http://reg.163.com/login.jsp?url=http://fm163.163.com/coremail/fcg/ntesdoor2?
"
;
//
var url = "http://reg.163.com/in.jsp?url=http://mimg.163.com/oy_frommail.htm?";
url
+=
"
verifycookie%3D1%26
"
;
if
( secure){
user.autocomplete
=
"
on
"
;
}
else
{
user.autocomplete
=
"
off
"
;
}
fGetVersion(fm);
fm.action
=
url
+
"
language%3D-1%26style%3D
"
+
fm.style.value;
visitordata.setVals( [fm.username.value,fm.style.value,fm.secure.checked
?
1
:
0
], fm.remUser.checked
?
true
:
false
);
visitordata.store();
return
true
;
}
function
fGetVersion(fm)
{
try
{
var
userAgent
=
window.navigator.userAgent;
var
AppName
=
""
;
var
Version
=
""
;
if
(userAgent.indexOf(
"
MSIE
"
)
>-
1
){
AppName
=
"
msie
"
;
Version
=
userAgent.replace(
/
.
+
MSIE
/
gi,
""
).replace(
/
;.
+/
gi,
""
)
-
0
;
}
else
if
(userAgent.toUpperCase().indexOf(
"
FIREFOX
"
)
>-
1
){
AppName
=
"
firefox
"
;
Version
=
userAgent.replace(
/
.
+
Firefox\
//
gi,"").replace(/\(.*\)/g,"") - 0;
}
else
if
(userAgent.toUpperCase().indexOf(
"
NETSCAPE
"
)
>-
1
){
AppName
=
"
netscape
"
;
Version
=
userAgent.replace(
/
.
+
NETSCAPE\
//
gi,"").replace(/\(.*\)/g,"") - 0;
}
if
((AppName
==
"
msie
"
&&
Version
<
5
)
||
(AppName
==
"
firefox
"
&&
Version
<
1
)
||
(AppName
==
"
netscape
"
&&
Version
<
7.2
)
||
userAgent.indexOf(
"
macintosh
"
)
!=
-
1
||
userAgent.indexOf(
"
konqueror
"
)
!=
-
1
||
userAgent.indexOf(
"
safari
"
)
!=
-
1
||
userAgent.indexOf(
"
camino
"
)
!=
-
1
){
fm.style.value
=
"
34
"
;
}
}
catch
(e){}
}
function
Cookie( document, name, domain )
{
this
.$document
=
document;
this
.$name
=
name;
this
.$expiration
=
new
Date(
2099
,
12
,
31
);
this
.$domain
=
domain;
this
.data
=
null
;
}
Cookie.prototype.store
=
function
()
{
var
cookieval
=
""
;
if
(
this
.data
!=
null
)
{
for
(
var
i
=
0
; i
<
this
.data.length;i
++
)
{
cookieval
+=
this
.data[i].join(
"
:
"
)
+
"
&
"
;
}
}
if
( cookieval
!=
""
&&
cookieval.charAt( cookieval.length
-
1
)
==
"
&
"
)
cookieval
=
cookieval.substring(
0
,cookieval.length
-
1
);
var
cookie
=
this
.$name
+
"
=
"
+
cookieval
+
"
;expires=
"
+
this
.$expiration.toGMTString()
+
"
;domain=
"
+
this
.$domain;
window.document.cookie
=
cookie;
}
Cookie.prototype.load
=
function
()
{
var
allcookies
=
this
.$document.cookie;
if
( allcookies
==
""
)
return
false
;
var
start
=
allcookies.indexOf(
this
.$name
+
"
=
"
);
if
( start
==
-
1
)
return
false
;
start
+=
this
.$name.length
+
1
;
var
end
=
allcookies.indexOf(
"
;
"
, start );
if
( end
==
-
1
) end
=
allcookies.length;
var
cookieval
=
allcookies.substring( start, end );
var
a
=
cookieval.split(
"
&
"
);
for
(
var
i
=
0
;i
<
a.length; i
++
)
a[i]
=
a[i].split(':');
//
用户名:风格:安全
this
.data
=
a;
return
true
;
}
Cookie.prototype.setVals
=
function
( a, flag )
{
if
(
this
.data
==
null
)
{
if
( flag )
{
this
.data
=
[];
this
.data[
0
]
=
a;
}
}
else
{
this
.data[
0
][
0
]
=
a[
0
];
if
( flag)
return
;
else
this
.data
=
null
;
}
}
function
fInitUserName()
{
var
fm
=
window.document.login163;
var
name
=
""
;
if
( visitordata.data
!=
null
)
{
name
=
visitordata.data[
0
][
0
];
fm.remUser.checked
=
true
;
fm.username.autocomplete
=
"
on
"
;
//
fm.secure.checked = (visitordata.data[0][3]==1);
}
else
{
fm.username.autocomplete
=
"
off
"
;
}
fm.username.focus();
if
( name
!=
""
){
fm.username.focus();
fm.username.value
=
name;
}
}
function
fTrim(str)
{
return
str.replace(
/
(
^
\s
*
)
|
(\s
*
$)
/
g,
""
);
}
var
visitordata
=
new
Cookie( document,
"
nts_mail_user
"
, document.domain);
visitordata.load();
1,先来分析fTrim()函数,这个函数的功能是过滤字符串两端的空格,类似于VBS里的Trim()函数.在这里他主要是通过正则表达式来实现的,正则表达式具有功能强大,代码短小精悍的特点,昨天偶专门学习了下正则表达式的用法,这里匹配模式为
/(^\s*)|(\s*$)/g,两端斜线"/"和"/"表示斜线内部是正则表达式的匹配模式,中间"|"表示两端括号内内容匹配其中之一就算匹配到.
g表示全文检索,如果不写这个g则匹配到第一个符合要求的字符串就停止检索.如上面的匹配模式如果没有g,(若左端有空格)则只会过滤左端空格,而不过滤右端.用()括住的部分表示这里是一组内容.第一个括号内^表示匹配内容必须从最左端开始,\s表示匹配空格,制表符,换行符.*表示匹配零个或多个前面的字符.第二个括号内的$表示匹配内容在结尾部分.这样匹配模式就可以过滤字符串两端的空格了.
2,再看fLoginFormSubmit()函数.
var ati = user.value.indexOf( "@");
if( ati != -1 ){
user.value = user.value.substring(0, ati);
}表示如果用户输入的是邮箱全称,则过滤@及其右端内容.
3,函数function fGetVersion():
用window.navigator.userAgent得到浏览器全称,如偶的电脑得到Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon) ,然后通过userAgent.indexOf("MSIE")>-1判断是否是ie,如果找到则返回其相应索引位置,否则返回-1,Version = userAgent.replace(/.+MSIE/gi,"").replace(/;.+/gi,"") - 0;利用正则表达式先过滤MSIE及其之前所有内容,然后过滤剩余内容所有以;开始的所有内容,剩余内容即为浏览器版本号,如偶的是6.0.
if((AppName == "msie" && Version < 5) || (AppName == "firefox" && Version <1) || (AppName == "netscape" && Version<7.2) || userAgent.indexOf("macintosh") != -1 || userAgent.indexOf("konqueror") != -1 || userAgent.indexOf("safari") != -1 || userAgent.indexOf("camino") != -1)对浏览器进行完整判断,对苹果linux及ie下低版本浏览器进行了完整的判断.
4,另外这里利用prototype属性对用户自定义cookie对象进行了扩展.
偶学习之后写的测试代码:
<script type="text/javascript">
var useragent=window.navigator.userAgent;
version=useragent.replace(/.+MSIE/gi,"").replace(/;.+/gi,"");
document.write(useragent);
alert(version);
var str=" testspace ";
str1=str.replace(/(^\s*)|(\s*$)/g,"");
alert("\""+str+"\"");
alert("\""+str1+"\"");
</script>