JavaScript适用小技巧2
JavaScript实用小技巧2
1.访问剪贴板
2.调用Windows自带的配色控件
3.document.location对象
4.字典对象Dictionary
5.调用iframe中的js
(1)iframe中,取父窗口中的数据
(2)调用iframe中的方法
6.取字符串的字符长度
7、15位身份证转成18位
8、检验身份证并返回信息
9、日期操作
10、判断img是否加载完成
11、无提示关闭窗口
1.访问剪贴板
window.clipboardData.setData("Text",oSource.innerText); window.clipboardData.getData("Text");
2.调用Windows自带的配色控件
<OBJECT id=dlgHelper CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></OBJECT> <script> var tempColor = "#0099CC"; function returnColor(){ var selColor, hexColor = dlgHelper.ChooseColorDlg(tempColor).toString(16); selColor = tempColor = ((hexColor.length<6)?"000000".substring(0,6-hexColor.length):"") + hexColor; with(event.srcElement){ value = selColor; style.backgroundColor = selColor; } } </script> <input type="text" value="#0099CC" size="12" onclick="returnColor()" style="background-color: #0099CC">
3.document.location对象
document.location.hash // #号后的部分 document.location.host // 域名+端口号 document.location.hostname // 域名 document.location.href // 完整URL document.location.pathname // 目录部分 document.location.port // 端口号 document.location.protocol // 网络协议(http:) document.location.search // ?号后的部分 documeny.location.reload() //刷新网页 document.location.reload(URL) //打开新的网页 document.location.assign(URL) //打开新的网页 document.location.replace(URL) //打开新的网页
4.字典对象Dictionary
var gDic = new ActiveXObject("Scripting.Dictionary"); //遍历Dictionary function printAll(dic){ var keys=dic.Keys().toArray(); for(var i = 0;i<keys.length;i++){ if(dic.Exists(keys[i])){ alert(dic.item(keys[i]).id); alert(dic(keys[i]).name); } } } function test(){ gDic.add('SN-001', {id:'001', name:'张三'}); gDic.add('SN-002', {id:'002', name:'李四'}); gDic.add('SN-003', {id:'003', name:'王五'}); printAll(gDic); gDic.remove('SN-002'); printAll(gDic); gDic.removeAll(); }
5.调用iframe中的js
(1)iframe中,取父窗口中的数据
var gData = parent.PrtData;
(2)调用iframe中的方法
$('#frm')[0].contentWindow.printView();
6.取字符串的字符长度
function countCharacters(str){ var len=0; for(i=0; i<str.length; i++) { if(!/[u00-uFF]/.test(str.charAt(i))) len = len + 2; else len = len + 1; } return len; }
7、15位身份证转成18位
/** * 将15位身份证转成18位:不做验证 */ function convertID15To18( cardID ){ if(cardID.length != 15) return; var v = new Array(); var vs = "10X98765432"; var newCardID = ""; v.push(2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7); var cardID17 = cardID.substring(0,6)+"19"+cardID.substring(6); var N = 0; var R = -1; var T = '0';//储存最后一个数字 var j = 0; var cardID18=""; //计数出第18位数字 for (var i = 16; i >= 0; i--){ N += parseInt(cardID17.substring(i, i + 1)) * v[j]; j++; } R = N % 11; T = vs.charAt(R); cardID18 = cardID17 + T; return cardID18; }
8、检验身份证并返回信息
function checkID18Card( sId ){ var result = {flag: true, msg: '验证通过'}; var area={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古", 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东", 41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西", 46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南", 54:"*",61:"陕西",62:"甘肃",63:"青海",64:"宁夏", 65:"*",71:"*",81:"香港",82:"澳门",91:"国外" } var iSum=0; var info=""; if(!/^\d{17}(\d|x)$/i.test(sId)){ result.flag = false; result.msg = "字符长度错误"; return result; } sId=sId.replace(/x$/i,"a"); if(area[parseInt(sId.substr(0,2))]==null){ result.flag = false; result.msg = "非法地区"; return result; } var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2)); var d=new Date(sBirthday.replace(/-/g,"/")) if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){ result.flag = false; result.msg = "非法生日"; return result; } for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) if(iSum%11!=1){ result.flag = false; result.msg = "非法证号"; return result; } result.area = area[parseInt(sId.substr(0,2))]; result.birthday = sBirthday; result.sex = sId.substr(16,1)%2?"男":"女"; return result; }
9、日期操作
Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth() + 1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth() + 3) / 3), //季度 "S" : this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } Date.prototype.addDays = function(d){ this.setDate(this.getDate() + d); } Date.prototype.addWeeks = function(w){ this.addDays(w * 7); } Date.prototype.addMonths= function(m){ var d = this.getDate(); this.setMonth(this.getMonth() + m); if (this.getDate() < d) this.setDate(0); } Date.prototype.addYears = function(y){ var m = this.getMonth(); this.setFullYear(this.getFullYear() + y); if (m < this.getMonth()) { this.setDate(0); } } Date.prototype.getWeekdayDate = function(weekday){ weekday %= 7; var time = this.getTime(); var diff = weekday - this.getDay(); time += diff*24*3600000; return new Date(time); }
10、判断img是否加载完成
function imgLoad(img,callback){ img.complete || img.readyState == 'loading' || img.readyState == 'complete' ? callback() : img.onload = callback; } /** * 取合适大小的图片 * orgin: 真实的图片大小 * limit: 限定范围大小 */ function getImageFitScale(origin, limit){ var _new = {}; var ratio = Math.min( 1, Math.max( 0, limit.width ) / origin.width || 1, Math.max( 0, limit.height ) / origin.height || 1); _new['margin-top'] = (limit.height - Math.round( origin.height * ratio ))/2; //_new['margin-left'] = (limit.width - Math.round( origin.width * ratio ))/2; _new.width = Math.round( origin.width * ratio ); _new.height = Math.round( origin.height * ratio ); return _new; } $("<img id='previewPic'/>").attr({src: data}).one('load', function(){ var _size = getImageFitScale({width:this.width, height:this.height}, gImageScale); $(this).css(_size); }); function showPic(){ var img = new Image(); img.src = getFtpPath(); $(img).load(function(){ var scale = getImageFitScale({width:this.width, height:this.height},{width:900,height:512}); $("#middle").append($("<img>",{ id: "zjpic", src: img.src, width: scale.width, height: scale.height })); } }
11、无提示关闭窗口
function closeWindowSilently(){ window.opener=null; window.open('','_self',''); window.close(); } function closeTopWinSilently(){ window.opener=null; window.open('','_top',''); window.parent.close(); }