现在的位置: 首页 > 综合 > 正文

javascript 中一些经典函数

2013年02月21日 ⁄ 综合 ⁄ 共 4908字 ⁄ 字号 评论关闭

总结一些javascript经典函数:
  为了节约版面,也为了加快下载速度,有些函数写在了一行,读者可以粘出来仔细查看,我认为比较好的几个函数,与大家共勉。

1、获取字符串长度 

function getStringLength(str){
  var endvalue
=0;
  var sourcestr
=new String(str);
  var tempstr;
  
for (var strposition = 0; strposition < sourcestr.length; strposition ++{
    tempstr
=sourcestr.charAt(strposition);
    
if (tempstr.charCodeAt(0)>255 || tempstr.charCodeAt(0)<0{
      endvalue
=endvalue+2;
    }
 else {
      endvalue
=endvalue+1;
    }

  }

  
return(endvalue);
}

2、去除字符串两端空格

function Trim(s){var m = s.match(/^/s*(/S+(/s+/S+)*)/s*$/);return (m == null)?"":m[1];}

 

 3、将字符串进行U编码与解码

function StrCode(str){if(encodeURIComponent) return encodeURIComponent(str);if(escape) return escape(str);}
function UnStrCode(str)
{if(decodeURIComponent ) return decodeURIComponent (str);if(unescape) return unescape(str);}

4、将字符串进行HtmlEncode与HtmlDecode操作

function HtmlEncode(text){var re = {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'};for (i in re) text = text.replace(new RegExp(i,'g'), re[i]);return text;}
function HtmlDecode(text)
{var re = {'&lt;':'<','&gt;':'>','&amp;':'&','&quot;':'"'};for (i in re) text = text.replace(new RegExp(i,'g'), re[i]);return text;}

 5、获取对象 (主要解决多种浏览器兼容问题)

function GetId(id){return document.getElementById?document.getElementById(id):null;}

function GetName(id)
{return document.getElementsByName?document.getElementsByName(id):null;}
function GetNames(name)
{return document.getElementsByTagName?document.getElementsByTagName(name):new Array()}

6、检查游览器 (isIE,isNS) 

function Browser(){var ua, s, i;this.isIE = false;this.isNS = false;this.isOP = false;this.isSF = false;ua = navigator.userAgent.toLowerCase();s = "opera";if ((i = ua.indexOf(s)) >= 0){this.isOP = true;return;}= "msie";if ((i = ua.indexOf(s)) >= 0{this.isIE = true;return;}= "netscape6/";if ((i = ua.indexOf(s)) >= 0{this.isNS = true;return;}= "gecko";if ((i = ua.indexOf(s)) >= 0{this.isNS = true;return;}= "safari";if ((i = ua.indexOf(s)) >= 0{this.isSF = true;return;}}

7、ajax异步数据访问

function InitRequest(){var C_req = null;try{C_req = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{C_req = new ActiveXObject("Microsoft.XMLHTTP");}catch(oc){C_req = null;}}if (!C_req && typeof XMLHttpRequest != "undefined"){try{C_req = new XMLHttpRequest();}catch(fa){alert("对不起!您的浏览器不支持该功能,请使用Internet Explorer 6.0或FireFox浏览器!");C_req = null;}}return C_req;}
function PostRequest(url, data)
{var AjaxRequestObj = InitRequest();if (AjaxRequestObj != null){AjaxRequestObj.onreadystatechange = function (){if (AjaxRequestObj.readyState == 4 && AjaxRequestObj.responseText){ProcessAjaxData(AjaxRequestObj.responseText);}};AjaxRequestObj.open("POST", url, true);AjaxRequestObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");AjaxRequestObj.send(data);}}
function ProcessAjaxData(data)
{eval(data);}

 8、用层显示一个弹出窗口
  注:页面上需要有<div id="ScreenOver"></div><div id="DialogMove"></div>

function ScreenConvert(){var browser = new Browser();var objScreen = GetId("ScreenOver");if(!objScreen) var objScreen = document.createElement("div");var oS = objScreen.style;objScreen.id = "ScreenOver";oS.display = "block";oS.top = oS.left = oS.margin = oS.padding = "0px";if (document.body.clientHeight)    {var wh = document.body.clientHeight + "px";}else if (window.innerHeight){var wh = window.innerHeight + "px";}else{var wh = "100%";}oS.width = "100%";oS.height = wh;oS.position = "absolute";oS.zIndex = "3";if ((!browser.isSF) && (!browser.isOP)){oS.background = "#cccccc";}else{oS.background = "#cccccc";}oS.filter = "alpha(opacity=40)";oS.opacity = 40/100;oS.MozOpacity = 40/100;document.body.appendChild(objScreen);var allselect = GetNames("select");for (var i=0; i<allselect.length; i++) allselect[i].style.visibility = "hidden";}
function ScreenClean()
{var objScreen = GetId("ScreenOver");if (objScreen) objScreen.style.display = "none";var allselect = GetNames("select");for (var i=0; i<allselect.length; i++) allselect[i].style.visibility = "visible";}
var t_DiglogX,t_DiglogY,t_DiglogW,t_DiglogH;
function DialogLoc()
{var dde = document.documentElement;if (window.innerWidth){var ww = window.innerWidth;var wh = window.innerHeight;var bgX = window.pageXOffset;var bgY = window.pageYOffset;}else{var ww = dde.offsetWidth;var wh = dde.offsetHeight;var bgX = dde.scrollLeft;var bgY = dde.scrollTop;}t_DiglogX = (bgX + ((ww - t_DiglogW)/2));t_DiglogY = (bgY + ((wh - t_DiglogH)/2));}
function DialogShow(showdata,ow,oh,w,h)
{var objDialog = GetId("DialogMove");if (!objDialog) objDialog = document.createElement("div");t_DiglogW = ow;t_DiglogH = oh;DialogLoc();objDialog.id = "DialogMove";var oS = objDialog.style;oS.display = "block";oS.top = t_DiglogY + "px";oS.left = t_DiglogX + "px";oS.margin = "0px";oS.padding = "0px";oS.width = w + "px";oS.height = h + "px";oS.position = "absolute";oS.zIndex = "5";oS.background = "#FFF
【上篇】
【下篇】

抱歉!评论已关闭.