js常用函數(shù)2008-8-16整理
/*------------------------------------------------------------
判斷輸入文本是否為身份證號碼,如為不正確則提示
text-------輸入的身份證號碼
使用例子onBlur="isPid(this)"
------------------------------------------------------------*/
function isPid(text)
{
var pid=text.value.Trim();
var temp="0123456789";
var temp1="0123456789xX";
if(pid!=""){
if(pid.length==15)
{
for(j=0; j<15; j++ )
{
var ch = pid.charAt(j);
if(temp.indexOf(ch)==-1)
{
alert("請輸入正確的身份證號碼!");
text.focus();
break;
}
}
}
else if(pid.length==18)
{
for(j=0; j<pid.length-1; j++ )
{
var ch = pid.charAt(j);
if(temp.indexOf(ch)==-1)
{
alert("請輸入正確的身份證號碼!");
text.focus();
break;
}
}
var ch1 = pid.charAt(pid.length-1);
if(temp1.indexOf(ch1)==-1)
{
alert("請輸入正確的身份證號碼!");
text.focus();
}
}
else{
alert("身份證號碼的應(yīng)為15位或18位!");
text.focus();
}}
}
/*------------------------------------------------------------
判斷兩次密碼輸入是否一致
text-------新密碼
name-------再次輸入新密碼
使用例子checkPassword(form1.newpass,form1.newpass1)
------------------------------------------------------------*/
function checkPassword(text,text1)
{
var newpass=text.value.Trim();
var newpass1=text1.value.Trim();
if(newpass!=newpass1){
alert("兩次輸入新密碼不一致!");
text.focus();
return true;
}
}
/**//*------------------------------------------------------------
判斷是否包含非法字符,如含非法字符則提示
text-------輸入文本
addtemp----除英文和數(shù)字外還可包含的字符
name-------提示的名字
include----提示中不允許包含的字符
使用例子onBlur="compareTwoDate(this,'@_','郵件','%*$')"
------------------------------------------------------------*/
function isChar(text,addtemp,name,include)
{
var temp="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+addtemp;
for(j=0; j<text.value.length; j++ )
{
var ch = text.value.Trim().charAt(j);
if(temp.indexOf(ch)==-1)
{
alert(name+"中不允許包含'"+include+"'等字符!");
text.focus();
break;
}
}
}
/**//*------------------------------------------------------------
判斷輸入的是否為電子郵件,如含非法字符則提示
text-------輸入的電子郵件
使用例子onBlur="isEmail(this)"
------------------------------------------------------------*/
function isEmail(text)
{
var email=text.value.Trim();
var m=email.indexOf("@");
var n=email.indexOf(".");
if(email!="")
{
if(m<1||m>email.length-3)
{
alert("請輸入正確的電子郵件格式!");
text.focus();
return true;
}
else if(n<m+2||n>email.length-2)
{
alert("請輸入正確的電子郵件格式!");
text.focus();
return true;
}
}
}
/**//*------------------------------------------------------------
判斷輸入文本是否為空,如為空則提示
text-------輸入文本
使用例子onBlur="isNull(this,'姓名')"
------------------------------------------------------------*/
function isNull(text,name)
{
if(text.value.Trim()==null||text.value.Trim()=="")
{
alert(name+"不能為空!");
text.focus();
return true;
}
}
//允許被框架
function enableFrame(){
if (top.location == self.location){
alert("該操作被管理員禁止");
document.execCommand("stop");
}
}
//禁止被框架
function disableFrame(obj){
if(top.location != self.location){
window.open(obj);
}
}
//根據(jù)ID控制層的隱藏或者展開
function show_hide(obj){
if($(obj).style.display == "none"){
$(obj).style.display = "";
}else{
$(obj).style.display = "none";
}
}
/*****屏幕*******************/
//調(diào)用方法:
//WinPage.getPageWidth
//WinPage.getBody().width
var WinPage = {
getPageWidth: function()
{
return document.body.scrollWidth || document.documentElement.scrollWidth || 0;
},
getPageHeight: function()
{
return document.body.scrollHeight || document.documentElement.scrollHeight || 0;
},
getBodyWidth: function()
{
return document.body.clientWidth || document.documentElement.clientWidth || 0;
},
getBodyHeight: function()
{
return document.documentElement.clientHeight || document.body.clientHeight || 0;
},
getBodyLeft: function()
{
return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
},
getBodyTop: function()
{
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
},
getBody: function()
{
return {
width : this.getBodyWidth(),
height : this.getBodyHeight(),
left : this.getBodyLeft(),
top : this.getBodyTop()
};
},
getScreenWidth: function()
{
return window.screen.width;
},
getScreenHeight: function()
{
return window.screen.height;
}
};
//測試瀏覽器類型//
var Browser = new Object();
Browser.ua = window.navigator.userAgent.toLowerCase();
Browser.ie = /msie/.test(Browser.ua);
Browser.moz = /gecko/.test(Browser.ua);
/****************/
//------------------用于載入一個文件-------------//
var JsLoader = {
load: function(sUrl, fCallback)
{
var _script = document.createElement("script");
_script.setAttribute("type", "text/javascript");
_script.setAttribute("src", sUrl);
document.getElementsByTagName("head")[0].appendChild(_script);
if (Browser.ie)
{
_script.onreadystatechange = function()
{
if (this.readyState=="loaded" || this.readyState=="complete")
{
fCallback();
}
};
}
else if (Browser.moz)
{
_script.onload = function()
{
fCallback();
};
}
else
{
fCallback();
}
}
};
//實現(xiàn)一個StringBuffer
// var sb = new StringBuffer();
//sb.append("<table border='1'>");
//sb.append("<tr><td>A</td><td>B</td></tr>");
//sb.append("</table>")
//document.write(sb.toString());
function StringBuffer() {
this._strings_ = new Array;
}
StringBuffer.prototype.append = function(str) {
this._strings_.push(str);
}
StringBuffer.prototype.toString = function() {
return this._strings_.join("");
}
StringBuffer.prototype.reset= function() {
this._strings_ = new Array;
}
相關(guān)文章
javascript跨域總結(jié)之window.name實現(xiàn)的跨域數(shù)據(jù)傳輸
本文給大家介紹window.name實現(xiàn)的跨域數(shù)據(jù)傳輸,自己親自實踐了一下,真的非常好用,特此分享到腳本之家網(wǎng)站供大家參考2015-11-11JavaScript語句錯誤throw、try及catch實例解析
這篇文章主要介紹了JavaScript語句錯誤throw、try及catch實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08js parentElement和offsetParent之間的區(qū)別
這里主要說的是 offsetParent 屬性,這個屬性在 MSDN 的文檔中也沒有解釋清楚,這就讓人更難理解這個屬性。 這幾天在網(wǎng)上找了些資料看看,再加上自己的一些測試,對此屬性有了那么一點的了解,在這里總結(jié)一下。2010-03-03