一直想写一个关于javascript 的服务器控件出来,先收集一些有用的脚本先!这样平日也可以用一下,免得平时需要的时候临时抱佛脚!
浏览器截获键盘输入:
/**
*方法名:onClickSelect
*描述:点中text框的时候,选中其中的文字
*输入:空
*输出:空
**/
function onClickSelect(){
var obj = document.activeElement;
if(obj.tagName == "TEXTAREA")
obj.select();
if(obj.tagName == "INPUT" )
if(obj.type == "text")
obj.select();
}
/**
触发函数onClickSelect(双击)
**/
document.ondbclick = onClickSelect;
/**
*方法名:onKeyDownDefault
*描述:用户在button以外的页面元素中按回车,转换为按tab键
*输入:空
*输出:空
**/
function onKeyDownDefault(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
else{
return true;
}
}
/**
*方法名:NumOnly()
*描述:只允许输入数字
*输入:空
*输出:空
**/
function NumOnly(keycode){
var i= window.event.keyCode
if (i>57 || i<48){
window.event.keyCode=27;
}
if (i==keycode){
window.event.keyCode=keycode;
}
}
/**
*方法名:keypress
*描述:按键控制
*输入:[inputType:是否只能从键盘上录入阿拉伯数字,类型string]
*输出:空
*modif by Lsb
**/
function keypress(inputType){
var i= window.event.keyCode;
switch (inputType)
{
case "onDigit" :
if (i<58 && i>47){window.event.keyCode=i;return;}
break;
case "onTime" :
if (i<58 && i>46){window.event.keyCode=i;return;}
break;
default :
var obj = window.event.srcElement;
if (i<58 && i>47){window.event.keyCode=i;return;}
if (obj.value.indexOf("-")==-1 && i==45){window.event.keyCode=45;return;}
if (obj.value.indexOf(".")==-1 && i==46){window.event.keyCode=46;return;}
break;
}
window.event.keyCode=27;
}
/**
*方法名:validDate
*描述:检验日期格式和日期是否合法
*输入:str(日期字符串);separator(日期分隔符)
*输出:true(false)是否为合格日期
*writen by GaoHong , 2002-11-6 10:12
**/
function validDate(str , separator){
if (separator == ""){
return false ;
}
if (str.indexOf(separator) == -1){
return false ;
}
if (str.indexOf(separator) == str.lastIndexOf(separator)){
return false ;
}
var year ;
var month ;
var date ;
year = str.substring(0 , str.indexOf(separator)) ;
month = str.substring(str.indexOf(separator) + 1 ,str.lastIndexOf(separator)) ;
date = str.substring(str.lastIndexOf(separator) + 1 , str.length) ;
year = parseInt(year , 10) ;
month = parseInt(month , 10) ;
date = parseInt(date , 10) ;
if (year >= 3000 || year <= 2000 || month > 12 || date > 31){
return false ;
}
var date1 = new Date() ;
//var date2 ;
date1.setFullYear(year) ;
date1.setMonth(month - 1) ;
date1.setDate(date) ;
var yearInt1 = date1.getFullYear() ;
var monthInt1 = date1.getMonth() ;
var dateInt1 = date1.getDate() ;
if (year == yearInt1 && month == monthInt1 + 1 && date == dateInt1){
return true ;
}else {
return false ;
}
}
/**
*方法名:validDateObj
*描述:判断obj中的日期是否合格,不合格则清除obj中的日期
*输入:obj
*输出:无
*writen by GaoHong , 2002-11-6 10:12
**/
function validDateObj(obj){
if (validDate(obj.value , '/') == false){
obj.value = "" ;
alert("日期不合法");
window.event.returnValue = false ;
}
}
/**
*方法名:do_confirm
*描述:如果确认,转向指定的URL
*输入:url,string
*输出:提示框
*writen by Cube316 , 2003-03-17 00:42
**/
function do_confirm(url,msg){
if(window.confirm(msg)){
window.location=url;
return true;
}
else{
return false;
}
}
/**
只能输入数字
**/
function onlynum(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入电话号码或者“-”
**/
function TelOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==109||event.keyCode==189||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入身份证号码或者“X”
**/
function IDCardOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==88||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入小数(包括小数点)
**/
function PriceOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==190||event.keyCode==110||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
弹出窗口
自定义长宽
自动聚焦
**/
function js_openwin(url,w,h) {
var newwin=window.open(url,"newwin","top=0,left=0,width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
newwin.focus();
}
/**
* Sets/unsets the pointer and marker in browse mode
*
* @param object the table row
* @param interger the row number
* @param string the action calling this script (over, out or click)
* @param string the default background color
* @param string the color to use for mouseover
* @param string the color to use for marking a row
*
* @return boolean whether pointer is set or not
*/
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
var theCells = null;
// 1. Pointer and mark feature are disabled or the browser can't get the
// row -> exits
if ((thePointerColor == '' && theMarkColor == '')
|| typeof(theRow.style) == 'undefined') {
return false;
}
// 2. Gets the current row and exits if the browser can't get it
if (typeof(document.getElementsByTagName) != 'undefined') {
theCells = theRow.getElementsByTagName('td');
}
else if (typeof(theRow.cells) != 'undefined') {
theCells = theRow.cells;
}
else {
return false;
}
// 3. Gets the current color
var rowCellsCnt = theCells.length;
var domDetect = null;
var currentColor = null;
var newColor = null;
// 3.1 with DOM compatible browsers except Opera that does not return
// valid values with "getAttribute"
if (typeof(window.opera) == 'undefined'
&& typeof(theCells[0].getAttribute) != 'undefined') {
currentColor = theCells[0].getAttribute('bgcolor');
domDetect = true;
}
// 3.2 with other browsers
else {
currentColor = theCells[0].style.backgroundColor;
domDetect = false;
} // end 3
// 4. Defines the new color
// 4.1 Current color is the default one
if (currentColor == ''
|| currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
if (theAction == 'over' && thePointerColor != '') {
newColor = thePointerColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor = theMarkColor;
marked_row[theRowNum] = true;
}
}
// 4.1.2 Current color is the pointer one
else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
&& (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
if (theAction == 'out') {
newColor = theDefaultColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor = theMarkColor;
marked_row[theRowNum] = true;
}
}
// 4.1.3 Current color is the marker one
else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
if (theAction == 'click') {
newColor = (thePointerColor != '')
? thePointerColor
: theDefaultColor;
marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
? true
: null;
}
} // end 4
// 5. Sets the new color
if (newColor) {
var c = null;
// 5.1 with DOM compatible browsers except Opera
if (domDetect) {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].setAttribute('bgcolor', newColor, 0);
} // end for
}
// 5.2 with other browsers
else {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].style.backgroundColor = newColor;
}
}
} // end 5
return true;
} // end of the 'setPointer()' function
/**
多重选中和取消复选框
*/
function setCheckboxes(the_form, do_check)
{
var elts = (typeof(document.forms[the_form].elements['selected_db[]']) != 'undefined')
? document.forms[the_form].elements['selected_db[]']
: document.forms[the_form].elements['selected_item[]'];
var elts_cnt = (typeof(elts.length) != 'undefined')
? elts.length
: 0;
if (elts_cnt) {
for (var i = 0; i < elts_cnt; i++) {
elts[i].checked = do_check;
} // end for
} else {
elts.checked = do_check;
} // end if else
return true;
}
*方法名:onClickSelect
*描述:点中text框的时候,选中其中的文字
*输入:空
*输出:空
**/
function onClickSelect(){
var obj = document.activeElement;
if(obj.tagName == "TEXTAREA")
obj.select();
if(obj.tagName == "INPUT" )
if(obj.type == "text")
obj.select();
}
/**
触发函数onClickSelect(双击)
**/
document.ondbclick = onClickSelect;
/**
*方法名:onKeyDownDefault
*描述:用户在button以外的页面元素中按回车,转换为按tab键
*输入:空
*输出:空
**/
function onKeyDownDefault(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
else{
return true;
}
}
/**
*方法名:NumOnly()
*描述:只允许输入数字
*输入:空
*输出:空
**/
function NumOnly(keycode){
var i= window.event.keyCode
if (i>57 || i<48){
window.event.keyCode=27;
}
if (i==keycode){
window.event.keyCode=keycode;
}
}
/**
*方法名:keypress
*描述:按键控制
*输入:[inputType:是否只能从键盘上录入阿拉伯数字,类型string]
*输出:空
*modif by Lsb
**/
function keypress(inputType){
var i= window.event.keyCode;
switch (inputType)
{
case "onDigit" :
if (i<58 && i>47){window.event.keyCode=i;return;}
break;
case "onTime" :
if (i<58 && i>46){window.event.keyCode=i;return;}
break;
default :
var obj = window.event.srcElement;
if (i<58 && i>47){window.event.keyCode=i;return;}
if (obj.value.indexOf("-")==-1 && i==45){window.event.keyCode=45;return;}
if (obj.value.indexOf(".")==-1 && i==46){window.event.keyCode=46;return;}
break;
}
window.event.keyCode=27;
}
/**
*方法名:validDate
*描述:检验日期格式和日期是否合法
*输入:str(日期字符串);separator(日期分隔符)
*输出:true(false)是否为合格日期
*writen by GaoHong , 2002-11-6 10:12
**/
function validDate(str , separator){
if (separator == ""){
return false ;
}
if (str.indexOf(separator) == -1){
return false ;
}
if (str.indexOf(separator) == str.lastIndexOf(separator)){
return false ;
}
var year ;
var month ;
var date ;
year = str.substring(0 , str.indexOf(separator)) ;
month = str.substring(str.indexOf(separator) + 1 ,str.lastIndexOf(separator)) ;
date = str.substring(str.lastIndexOf(separator) + 1 , str.length) ;
year = parseInt(year , 10) ;
month = parseInt(month , 10) ;
date = parseInt(date , 10) ;
if (year >= 3000 || year <= 2000 || month > 12 || date > 31){
return false ;
}
var date1 = new Date() ;
//var date2 ;
date1.setFullYear(year) ;
date1.setMonth(month - 1) ;
date1.setDate(date) ;
var yearInt1 = date1.getFullYear() ;
var monthInt1 = date1.getMonth() ;
var dateInt1 = date1.getDate() ;
if (year == yearInt1 && month == monthInt1 + 1 && date == dateInt1){
return true ;
}else {
return false ;
}
}
/**
*方法名:validDateObj
*描述:判断obj中的日期是否合格,不合格则清除obj中的日期
*输入:obj
*输出:无
*writen by GaoHong , 2002-11-6 10:12
**/
function validDateObj(obj){
if (validDate(obj.value , '/') == false){
obj.value = "" ;
alert("日期不合法");
window.event.returnValue = false ;
}
}
/**
*方法名:do_confirm
*描述:如果确认,转向指定的URL
*输入:url,string
*输出:提示框
*writen by Cube316 , 2003-03-17 00:42
**/
function do_confirm(url,msg){
if(window.confirm(msg)){
window.location=url;
return true;
}
else{
return false;
}
}
/**
只能输入数字
**/
function onlynum(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入电话号码或者“-”
**/
function TelOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==109||event.keyCode==189||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入身份证号码或者“X”
**/
function IDCardOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==88||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
只能输入小数(包括小数点)
**/
function PriceOnly(){
if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false){
if (window.event.srcElement.type != "button")
window.event.keyCode = 9;
}
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||event.keyCode==8||event.keyCode==9||event.keyCode==46||event.keyCode==190||event.keyCode==110||event.keyCode==37||event.keyCode==39))
event.returnValue=false;
}
/**
弹出窗口
自定义长宽
自动聚焦
**/
function js_openwin(url,w,h) {
var newwin=window.open(url,"newwin","top=0,left=0,width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
newwin.focus();
}
/**
* Sets/unsets the pointer and marker in browse mode
*
* @param object the table row
* @param interger the row number
* @param string the action calling this script (over, out or click)
* @param string the default background color
* @param string the color to use for mouseover
* @param string the color to use for marking a row
*
* @return boolean whether pointer is set or not
*/
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
var theCells = null;
// 1. Pointer and mark feature are disabled or the browser can't get the
// row -> exits
if ((thePointerColor == '' && theMarkColor == '')
|| typeof(theRow.style) == 'undefined') {
return false;
}
// 2. Gets the current row and exits if the browser can't get it
if (typeof(document.getElementsByTagName) != 'undefined') {
theCells = theRow.getElementsByTagName('td');
}
else if (typeof(theRow.cells) != 'undefined') {
theCells = theRow.cells;
}
else {
return false;
}
// 3. Gets the current color
var rowCellsCnt = theCells.length;
var domDetect = null;
var currentColor = null;
var newColor = null;
// 3.1 with DOM compatible browsers except Opera that does not return
// valid values with "getAttribute"
if (typeof(window.opera) == 'undefined'
&& typeof(theCells[0].getAttribute) != 'undefined') {
currentColor = theCells[0].getAttribute('bgcolor');
domDetect = true;
}
// 3.2 with other browsers
else {
currentColor = theCells[0].style.backgroundColor;
domDetect = false;
} // end 3
// 4. Defines the new color
// 4.1 Current color is the default one
if (currentColor == ''
|| currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
if (theAction == 'over' && thePointerColor != '') {
newColor = thePointerColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor = theMarkColor;
marked_row[theRowNum] = true;
}
}
// 4.1.2 Current color is the pointer one
else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
&& (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
if (theAction == 'out') {
newColor = theDefaultColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor = theMarkColor;
marked_row[theRowNum] = true;
}
}
// 4.1.3 Current color is the marker one
else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
if (theAction == 'click') {
newColor = (thePointerColor != '')
? thePointerColor
: theDefaultColor;
marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
? true
: null;
}
} // end 4
// 5. Sets the new color
if (newColor) {
var c = null;
// 5.1 with DOM compatible browsers except Opera
if (domDetect) {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].setAttribute('bgcolor', newColor, 0);
} // end for
}
// 5.2 with other browsers
else {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].style.backgroundColor = newColor;
}
}
} // end 5
return true;
} // end of the 'setPointer()' function
/**
多重选中和取消复选框
*/
function setCheckboxes(the_form, do_check)
{
var elts = (typeof(document.forms[the_form].elements['selected_db[]']) != 'undefined')
? document.forms[the_form].elements['selected_db[]']
: document.forms[the_form].elements['selected_item[]'];
var elts_cnt = (typeof(elts.length) != 'undefined')
? elts.length
: 0;
if (elts_cnt) {
for (var i = 0; i < elts_cnt; i++) {
elts[i].checked = do_check;
} // end for
} else {
elts.checked = do_check;
} // end if else
return true;
}
浏览器截获键盘输入:
<html>
<!--//this code by hongseheike-->
<head>
<script language="javascript">
ns4 = (document.layers) ? true : false;
ie4 = (document.all) ? true : false;
function keyDown(e){
if(ns4){
var nkey=e.which;
var iekey='现在是ns浏览器';
var realkey=String.fromCharCode(e.which);
}
if(ie4){
var iekey=event.keyCode;
var nkey='现在是ie浏览器';
var realkey=String.fromCharCode(event.keyCode);
if(event.keyCode==32){realkey='\' 空格\''}
if(event.keyCode==13){realkey='\' 回车\''}
if(event.keyCode==27){realkey='\' Esc\''}
if(event.keyCode==16){realkey='\' Shift\''}
if(event.keyCode==17){realkey='\' Ctrl\''}
if(event.keyCode==18){realkey='\' Alt\''}
}
alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
}
document.onkeydown = keyDown;
if(ns4){
document.captureEvents(Event.KEYDOWN);}
</script>
</head>
<body>
//Javascript Document
<hr>
<center>
<h3>请按任意一个键。。。。</h3>
</center>
</body>
</html>
<!--//this code by hongseheike-->
<head>
<script language="javascript">
ns4 = (document.layers) ? true : false;
ie4 = (document.all) ? true : false;
function keyDown(e){
if(ns4){
var nkey=e.which;
var iekey='现在是ns浏览器';
var realkey=String.fromCharCode(e.which);
}
if(ie4){
var iekey=event.keyCode;
var nkey='现在是ie浏览器';
var realkey=String.fromCharCode(event.keyCode);
if(event.keyCode==32){realkey='\' 空格\''}
if(event.keyCode==13){realkey='\' 回车\''}
if(event.keyCode==27){realkey='\' Esc\''}
if(event.keyCode==16){realkey='\' Shift\''}
if(event.keyCode==17){realkey='\' Ctrl\''}
if(event.keyCode==18){realkey='\' Alt\''}
}
alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
}
document.onkeydown = keyDown;
if(ns4){
document.captureEvents(Event.KEYDOWN);}
</script>
</head>
<body>
//Javascript Document
<hr>
<center>
<h3>请按任意一个键。。。。</h3>
</center>
</body>
</html>