使用JavaScript對資料進行編碼和解碼動作,代碼如下:
<mce:script type="text/javascript"><!--<br />var Url = {</p><p>// public method for url encoding<br />encode : function (string) {<br />return escape(this._utf8_encode(string));<br />},</p><p>// public method for url decoding<br />decode : function (string) {<br />return this._utf8_decode(unescape(string));<br />},</p><p>// private method for UTF-8 encoding<br />_utf8_encode : function (string) {<br />string = string.replace(//r/n/g,"/n");<br />var utftext = "";</p><p>for (var n = 0; n < string.length; n++) {</p><p>var c = string.charCodeAt(n);</p><p>if (c < 128) {<br />utftext += String.fromCharCode(c);<br />}<br />else if((c > 127) && (c < 2048)) {<br />utftext += String.fromCharCode((c >> 6) | 192);<br />utftext += String.fromCharCode((c & 63) | 128);<br />}<br />else {<br />utftext += String.fromCharCode((c >> 12) | 224);<br />utftext += String.fromCharCode(((c >> 6) & 63) | 128);<br />utftext += String.fromCharCode((c & 63) | 128);<br />}</p><p>}</p><p>return utftext;<br />},</p><p>// private method for UTF-8 decoding<br />_utf8_decode : function (utftext) {<br />var string = "";<br />var i = 0;<br />var c = c1 = c2 = 0;</p><p>while ( i < utftext.length ) {</p><p>c = utftext.charCodeAt(i);</p><p>if (c < 128) {<br />string += String.fromCharCode(c);<br />i++;<br />}<br />else if((c > 191) && (c < 224)) {<br />c2 = utftext.charCodeAt(i+1);<br />string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));<br />i += 2;<br />}<br />else {<br />c2 = utftext.charCodeAt(i+1);<br />c3 = utftext.charCodeAt(i+2);<br />string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));<br />i += 3;<br />}</p><p>}</p><p>return string;<br />}</p><p>}</p><p>var sss = "http://192.168.5.194/html/base/base_qry_main.php?new=1&time[0][0]=+&time[0][1]=%3E%3D&time[0][2]=12&time[0][3]=29&time[0][4]=2010&time[0][5]=&time[0][6]=&time[0][7]=&time[0][8]=+&time[0][9]=+&submit=Query+DB&num_result_rows=-1&time_cnt=1";</p><p>alert(Url.decode(sss));<br />//alert(sss.replace("&","&","ig"));<br />//alert(Url.decode(sss.replace("&","&","ig")));<br />// --></mce:script>
使用JavaScript對IP地址進行校正的代碼如下:
<mce:script type="text/javascript"><!--<br />String.prototype.isIP = function(){<br /> var reSpaceCheck = /^([0-9]{1,3})/.([0-9]{1,3})/.([0-9]{1,3})/.([0-9]{1,3})$/i;<br /> if (reSpaceCheck.test(this)){<br /> this.match(reSpaceCheck);<br /> if (RegExp.$1 <= 255 && RegExp.$1 > 0<br /> && RegExp.$2 <= 255 && RegExp.$2 >= 0<br /> && RegExp.$3 <= 255 && RegExp.$3 >= 0<br /> && RegExp.$4 <= 255 && RegExp.$4 >= 0) {<br /> return true;<br /> } else {<br /> return false;<br /> }<br /> } else {<br /> return false;<br /> }<br />}<br />var strip = "0.0.5.194";<br />alert(strip.isIP());<br />// --></mce:script>
使用JavaScript代碼進行清除空格動作的JavaScript代碼如下:
String.prototype.Trim = function()
{
return this.replace(/(^/s*)|(/s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^/s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(/s*$)/g, "");
}