javascript十个最常用的自定义函数
2009-09-08 00:00:00 来源:WEB开发网另一个版本:
1.var inArray = function (arr,value) {
2. for (var i=0,l = arr.length ; i <l ; i++) {
3. if (arr[i] === value) {
4. return true;
5. }
6. }
7. return false;
8.};
(3) getCookie(), setCookie(), deleteCookie()
做BBS与商业网站的应该经常用到,无理由每次都要让用户输入密码登录吧。我们需要借助cookie实现自动登录功能。
01.function getCookie( name ) {
02. var start = document.cookie.indexOf( name + "=" );
03. var len = start + name.length + 1;
04. if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
05. return null;
06. }
07. if ( start == -1 ) return null;
08. var end = document.cookie.indexOf( ';', len );
09. if ( end == -1 ) end = document.cookie.length;
10. return unescape( document.cookie.substring( len, end ) );
11.}
12.function setCookie( name, value, expires, path, domain, secure ) {
13. var today = new Date();
14. today.setTime( today.getTime() );
15. if ( expires ) {
16. expires = expires * 1000 * 60 * 60 * 24;
17. }
18. var expires_date = new Date( today.getTime() + (expires) );
19. document.cookie = name+'='+escape( value ) +
20. ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
21. ( ( path ) ? ';path=' + path : '' ) +
22. ( ( domain ) ? ';domain=' + domain : '' ) +
23. ( ( secure ) ? ';secure' : '' );
24.}
25.function deleteCookie( name, path, domain ) {
26. if ( getCookie( name ) ) document.cookie = name + '=' +
27. ( ( path ) ? ';path=' + path : '') +
28. ( ( domain ) ? ';domain=' + domain : '' ) +
29. ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
30.}
Tags:javascript 十个 常用
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接