在使用JS框架之前自己写的一些工具代码

来源:互联网 发布:mac painter 丙烯画笔 编辑:程序博客网 时间:2024/06/15 19:56
  1. //vb2005xu@sina.com版权所有 (*^__^*) 嘻嘻……   
  2.   
  3. //function -- Debug   
  4. function debug(msg)   
  5. {   
  6.     if (msg == undefined)   
  7.     {   
  8.         msg = 'Not Parameter' ;   
  9.     }   
  10.     alert('Debug: ' + msg);   
  11. }   
  12.   
  13. //function -- DOM   
  14. function $(id){return document.getElementById(id);}   
  15. function $name(ids){return document.getElementsByName(ids);}   
  16.   
  17. //function -- 操作 隐藏/显示 ; 禁用/恢复   
  18. function hide(obj){obj.style.display="";}   
  19. function unhide(obj){obj.style.display="none";}   
  20. function lock(obj){obj.disabled="disabled";}   
  21. function unlock(obj){obj.disabled="";}   
  22. function getFocus(obj){obj.focus();}   
  23. function setSelect(obj){obj.select();}   
  24.   
  25. //function -- Form 控件操作   
  26. function unlockTextInput(id){var obj=$(id);unlock(obj);getFocus(obj);setSelect(obj);}   
  27. function unlockSelectOption(id){var obj=$(id);unlock(obj);getFocus(obj);}   
  28. function checkboxVarity(ids) //验证多选框有无选中   
  29. {   
  30.     var checkboxobj = $name(ids);   
  31.     var selectState = false    ;   
  32.     for(i = 0 ; i < checkboxobj.length ; i++ )   
  33.     {   
  34.        if (checkboxobj[i].checked)   
  35.        {   
  36.             selectState = true ;   
  37.             break ;   
  38.        }   
  39.     }   
  40.     return selectState ;   
  41. }   
  42.   
  43. //function -- Cookie   
  44. function createCookie(name, value, days) {   
  45.     var expires = '';   
  46.     if (days) {   
  47.         var date = new Date();   
  48.         date.setTime(date.getTime() + (days*24*60*60*1000));   
  49.         var expires = '; expires=' + date.toGMTString();   
  50.     }   
  51.     document.cookie = name + '=' + value + expires + '; path=/';   
  52. }   
  53. function readCookie(name) {   
  54.     var cookieCrumbs = document.cookie.split(';');   
  55.     var nameToFind = name + '=';   
  56.     for (var i = 0; i < cookieCrumbs.length; i++) {   
  57.         var crumb = cookieCrumbs[i];   
  58.         while (crumb.charAt(0) == ' ') {   
  59.             crumb = crumb.substring(1, crumb.length); /* delete spaces */  
  60.         }   
  61.         if (crumb.indexOf(nameToFind) == 0) {   
  62.             return crumb.substring(nameToFind.length, crumb.length);   
  63.         }   
  64.     }   
  65.     return null;   
  66. }   
  67. function eraseCookie(name) {   
  68.     createCookie(name, '', -1);   
  69. }   
  70.   
  71.   
  72. //function -- Base64   
  73. function stringToBase64(string, padchar) {   
  74.     return window.btoa(string);   
  75. }   
  76.   
  77. function base64ToString(string) {   
  78.     string = string.replace(//s+/g, "");   
  79.     return window.atob(string);   
  80. }   
  81.   
  82. //function -- 操作Table   
  83. function getCurrentLineIndex(id){var obj=$(id);return obj.rowIndex;}   
  84. function getCurrentColumnIndex(id){var obj=$(id);return obj.cellIndex;}   
  85.   
  86.   
  87. //function -- 操作Code   
  88. function codeHightLight(id){var obj=$(id);obj.style.background='#fff';}   
  89.   
  90. //Ajax   
  91.        
  92.         //define a bool paramter to check IE instance   
  93.         var xmlhttp = false ;   
  94.   
  95.         //check client brower is IE   
  96.         try {   
  97.             // If javascript is greater than 5   
  98.             xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");   
  99.             //alert("You are using Microsoft Internet Explorer .");   
  100.         }   
  101.         catch (e){   
  102.             //else will use ActiveXObject older version   
  103.             try {   
  104.                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
  105.                 //alert("You are using old Microsoft Internet Explorer .");   
  106.             }   
  107.             catch (e){   
  108.                  //using brower is no IE.   
  109.                  xmlhttp = false ;   
  110.             }   
  111.         }   
  112.         if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {   
  113.             xmlhttp = new XMLHttpRequest();   
  114.             //alert("You are not using Microsoft Internet Explorer .");   
  115.         }   
  116.         function appendText(obj,xmlhttp)   
  117.         {   
  118.             if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ){   
  119.                     obj.innerHTML = xmlhttp.responseText ;   
  120.             }   
  121.         }   
  122.         function makerequest(serverpage,objID)   
  123.         {   
  124.              var obj = document.getElementById(objID);   
  125.              xmlhttp.open("GET",serverpage);   
  126.   
  127.              xmlhttp.onreadystatechange = appendText(obj,xmlhttp) ;   
  128.              xmlhttp.send(null) ;   
  129.         }   
  130.   
  131. /**  
  132.  * ###################  
  133.  * ###################        Lalestory 项目中抽取出来的JS功能函数--作者: 许消寒  
  134.  * ###################  
  135.  */    
  136.   
  137. /**  
  138.  * 功能:判断一个字符串是否为空字符串  
  139.  * 参数:str 检验值  
  140.  * 返回:TRUE OR FALSE  
  141.  */  
  142. function isBlank(str){   
  143.   if(str.length < 1){   
  144.     return true;   
  145.   }   
  146.   
  147.   for(i = 0; i < str.length; i ++){   
  148.     if(str.substring(i, i + 1) != ' '){   
  149.       return false;   
  150.     }   
  151.   }   
  152.      
  153.   return true;   
  154. }   
  155.   
  156.   
  157. /**  
  158.  * 功能:检验长度是否正确  
  159.  * 参数:str 检验值,minlen 最小长度,maxlen 最大长度  
  160.  * 返回:TRUE OR FALSE  
  161.  */  
  162. function checkByteLength(str,minlen,maxlen) {   
  163.     if (str == nullreturn false;                                  //为空返回false   
  164.     var l = str.length;   
  165.     var blen = 0;   
  166.     for(i=0; i<l; i++) {                                     //循环取得检验值的长度   
  167.         if ((str.charCodeAt(i) & 0xff00) != 0) {   
  168.             blen ++;   
  169.         }   
  170.         blen ++;   
  171.     }   
  172.     if (blen > maxlen || blen < minlen) {                         //判断长度是否合法   
  173.         return false;   
  174.     }   
  175.     return true;   
  176. }   
  177. /**  
  178.  * 功能:检验用户名是否合法  
  179.  * 参数:value 检验值  
  180.  * 返回:TRUE OR FALSE  
  181.  */  
  182. function validateUsername(value){   
  183.     var patn = /^[a-zA-Z]+[a-zA-Z0-9]+$/;    
  184.     //var patn = /^[^/s]*$/;   
  185.     if(!checkByteLength(value,4,20)) return true;                   //判断长度是否合法   
  186.     if(!patn.test(value)){                                      //判断格式是否合法   
  187.         return true;   
  188.     }   
  189.     return false;    
  190. }   
  191.   
  192.   
  193. /**  
  194.  * 功能:检验登陆或者注册的用户名 -- 用户名称至少为4个字符,但是少于20个字符  
  195.  * 参数:value 检验值  
  196.  * 返回:TRUE OR FALSE  
  197.  */  
  198. function checkUserName(value)   
  199. {   
  200.     if(value == '')                                         //判断用户名是否为空,返回false   
  201.     {   
  202.         return false;   
  203.     }   
  204.     if(validateUsername(value))                             //判断用户名是否合法   
  205.     {   
  206.         return false;   
  207.     }   
  208.     return true;   
  209. }   
  210.   
  211.   
  212. /**  
  213.  * 功能:检验Email是否合法  
  214.  * 参数:value 检验值  
  215.  * 返回:TRUE OR FALSE  
  216.  */  
  217. function validateEmail(value){   
  218.     var patn=/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/;   
  219.     if(!patn.test(value)) return false;                             //判断Email是否合法   
  220.     return true;   
  221. }   
  222. /**  
  223.  * 功能:检验Email  
  224.  * 参数:value 检验值  
  225.  * 返回:TRUE OR FALSE  
  226.  */  
  227. function CheckEmail(value)   
  228. {   
  229.     if(value == '')                                         //判断Email是否为空   
  230.     {   
  231.         return false;   
  232.     }   
  233.     if(!validateEmail(value))                                   //判断Email是否合法   
  234.     {   
  235.         return false  
  236.     }   
  237.     return true;   
  238. }   
  239.   
  240. /**  
  241.  * 功能: 检验是否包含特殊字符  
  242.  * 参数:value 检验值  
  243.  * 返回:TRUE OR FALSE  
  244.  */    
  245. function validateSpecSymbol(value) {   
  246.     return true ;//! /^[a-zA-Z0-9/u4E00-/u9FA5#/]*$/.test(value);   
  247. }   
  248.   
  249. /**  
  250.  * 功能: 检验是否包含特殊字符  
  251.  * 参数:value 检验值  
  252.  * 返回:TRUE OR FALSE  
  253.  */  
  254.   
  255. function CheckSpecSymbol(value)   
  256. {   
  257.     if(value == '')                                         //判断Email是否为空   
  258.     {   
  259.         return false;   
  260.     }   
  261.     if(!validateSpecSymbol(value))                                  //判断Email是否合法   
  262.     {   
  263.         return false ;   
  264.     }   
  265.     return true;       
  266. }    
  267. /**  
  268.  * 功能: 返回指定颜色的文本  
  269.  * 参数:content 文本 , colorValue为颜色值 , cssClass 为CSS风格  
  270.  * 返回:TRUE OR FALSE  
  271.  */  
  272. function getSpeciColorText(content,size,colorValue,cssClass)   
  273. {   
  274.     var str = "<font size='" + size + "' class='" ;   
  275.     str +=  cssClass + "' color='"  
  276.     str += colorValue + "'>" ;   
  277.     str += content + "</font>" ;   
  278.     return str ;   
  279. }   
  280. /**  
  281.  * 功能: 在指定的页面标签后放入一张图片  
  282.  * 参数:parentTagName为指定的页面标签 ,imgsrc为图片路径  
  283.  * 返回:TRUE OR FALSE  
  284.  */  
  285. function appendImg(parentTagName,imgsrc)   
  286. {   
  287.     //接收 document.all.yourTagName   
  288.     var parentTag = document.getElementsByName(parentTagName)[0] ;   
  289.     var str = "<img src='" + imgsrc + "'>" ;   
  290.     parentTagName.innerHTML = str ;   
  291. }   
  292.   
  293.   
  294. /* ################################## 在beijingextreme项目中的具体应用 ###############################*/  
  295.   
  296. //function -- 操作层   
  297. function $(elementID)   
  298. {   
  299.     return document.getElementById(elementID) ;   
  300. }   
  301.   
  302. function showDiv(divID)   
  303. {   
  304.     var divId = $(divID) ;   
  305.     divId.style.display = "" ;   
  306. }   
  307. function hideDiv(divID)   
  308. {   
  309.     var divId = $(divID) ;   
  310.     divId.style.display = "none" ;   
  311. }