JavaScript

来源:互联网 发布:apk软件下载中心 编辑:程序博客网 时间:2024/06/04 08:06
//$(window).resize(function(){}//JavaScript中的任何一个全局函数或变量都是window的属性。//var name="撼地神牛"; document.write(window.name);//self对象与window对象完全相同,self通常用于确认就是在当前的窗体内.window、self、window.self三者是等价的//document.write(window == self);      //必须相等,永远都相等//document.write(window.Top == window.self);  //判断当前框架是否是主框架//window的子对象    document对象  frames对象    history对象   location对象  navigator对象 screen对象//window.location子对象        location对象的属性有:href,protocal,host,hostname,port,pathname,search,hash//document.write(location.href + "<br/>");        // http://localhost:4889/javascriptTest.html//document.write(location.protocol + "<br/>");    // http://document.write(location.host + "<br/>");        // localhost:4889//document.write(location.hostname + "<br/>");    // localhost//document.write(location.port + "<br/>");        // 4889//document.write(location.pathname + "<br/>");    // /javascriptTest.html//document.write(location.search + "<br/>");  //http://localhost:4889/javascriptTest.html?id=1&name=张三 如果路径是这样,则输出  ?id=1&name=%E5%BC%A0%E4%B8%89//document.write(location.hash);//http: //localhost:4889/javascriptTest.html#kk=你好?id=1&name=张三 如果路径是这样,则输出  #kk=你好?id=1&name=张三//location.reload();  //重新加载页面//location.replace();  //本窗口载入新文档//location.assign() ;  //本窗口载入新文档//location = "http://www.baidu.com";  //跳转到指定网址//location = "search.html";        //相对路径跳转//location = "#top";      //跳转到页面顶部//浏览历史  History()对象的back()与forward() 与浏览器的“后退”,"前进"功能一样。//history.go(-2);  //后退两个历史记录//浏览器和屏幕信息 navigator.appName navigator.appVersion   navigator.userAgent    navagator.platform   //document.write(navigator.userAgent + "<br/>"); // Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11//document.write(navigator.appName + "<br/>");   //Netscape//document.write(navigator.appVersion + "<br/>"); //5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11//document.write(navigator.platform);             //Win32//时间等待与间隔函数://setTimeout()、clearTimeout()  在指定的时间后调用函数//setInterval()、clearInterval(value)  在间隔指定的事件后重复调用函数//对话框函数:alert() confirm()   prompt()//窗体函数:open()   close() //弹窗方法<body onload="openwin()"> 浏览器读页面时弹出窗口;<body onunload="openwin()"> 浏览器离开页面时弹出窗口;  //窗体焦点控制函数:focus() 函数:使窗体或空间获得焦点  blur() 函数:使窗体或控件失去焦点//窗体滚动轴控制函数: scrollTo() scrollBy()//窗体控制函数(仅对IE有效): moveBy()  moveTo()    resizeBy()  resizeTo()//event事件对象  最有用的两个操作:阻止事件冒泡。有时return false;不管用,这个或许就管用了。//IE://window.event.cancelBubble = true;//停止冒泡//window.event.returnValue = false;//阻止事件的默认行为//Firefox://event.preventDefault();// 取消事件的默认行为  //event.stopPropagation(); // 阻止事件的传播/*js函数传参 var obj1 = {value:'111'};var obj2 = {value:'222'}; function changeStuff(obj){obj.value = '333';obj = obj2;return obj.value;}  var foo = changeStuff(obj1); console.log(foo);// '222' 参数obj指向了新的对象obj2console.log(obj1.value);//'333' *//*javaScript对象this    全局函数apply和call可以用来改变函数中this的指向  prototypeconstructor*//*     数字格式化  alert(formatNumber(0,'.00')); alert(formatNumber(12432.21,'#,###')); alert(formatNumber(12432.21,'#,###.000#')); alert(formatNumber(12432,'#,###.00')); */ function formatNumber(number,pattern)    {     var str   = number.toString();     var strInt;     var strFloat;     var formatInt;     var formatFloat;     if(/\./g.test(pattern))     {      formatInt  = pattern.split('.')[0];      formatFloat  = pattern.split('.')[1];     }     else     {      formatInt  = pattern;      formatFloat  = null;     }     if(/\./g.test(str))     {      if(formatFloat!=null)      {       var tempFloat = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);       strInt  = (Math.floor(number)+Math.floor(tempFloat)).toString();           strFloat = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';         }      else      {       strInt  = Math.round(number).toString();       strFloat = '0';      }     }     else     {      strInt  = str;      strFloat = '0';     }     if(formatInt!=null)     {      var outputInt = '';      var zero  = formatInt.match(/0*$/)[0].length;      var comma  = null;      if(/,/g.test(formatInt))      {       comma  = formatInt.match(/,[^,]*/)[0].length-1;      }      var newReg  = new RegExp('(\\d{'+comma+'})','g');      if(strInt.length<zero)      {       outputInt  = new Array(zero+1).join('0')+strInt;       outputInt  = outputInt.substr(outputInt.length-zero,zero)      }      else      {       outputInt  = strInt;      }      var       outputInt   = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1')      outputInt   = outputInt.replace(/^,/,'');      strInt = outputInt;     }     if(formatFloat!=null)     {      var outputFloat = '';      var zero  = formatFloat.match(/^0*/)[0].length;      if(strFloat.length<zero)      {       outputFloat  = strFloat+new Array(zero+1).join('0');       //outputFloat  = outputFloat.substring(0,formatFloat.length);       var outputFloat1 = outputFloat.substring(0,zero);       var outputFloat2 = outputFloat.substring(zero,formatFloat.length);       outputFloat  = outputFloat1+outputFloat2.replace(/0*$/,'');      }      else      {       outputFloat  = strFloat.substring(0,formatFloat.length);      }      strFloat = outputFloat;     }     else     {      if(pattern!='' ||(pattern=='' && strFloat=='0'))      {       strFloat = '';      }     }     return strInt+(strFloat==''?'':'.'+strFloat);    }//alert(formatNumber(12432.419,'#,###.0#'));    /**       * 对Date的扩展,将 Date 转化为指定格式的String       * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符       * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)       * eg:       * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423       * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-1020:09:04       * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04       * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04       * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18       */        Date.prototype.pattern=function(fmt) {             var o = {             "M+" : this.getMonth()+1, //月份             "d+" : this.getDate(), //"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时             "H+" : this.getHours(), //小时             "m+" : this.getMinutes(), //"s+" : this.getSeconds(), //"q+" : Math.floor((this.getMonth()+3)/3), //季度             "S" : this.getMilliseconds() //毫秒             };             var week = {             "0" : "/u65e5",             "1" : "/u4e00",             "2" : "/u4e8c",             "3" : "/u4e09",             "4" : "/u56db",             "5" : "/u4e94",             "6" : "/u516d"            };             if(/(y+)/.test(fmt)){                 fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));             }             if(/(E+)/.test(fmt)){                 fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);             }             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;         }       //var date = new Date(); window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
0 0
原创粉丝点击