common.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * my-js Document
  3. * author : sunjiawei at 2010-10-8
  4. * mailto : sunjiawei1986@163.com
  5. */
  6. /******************** prototype ********************/
  7. var fn = Function.prototype;
  8. fn.extend = function(foo){
  9. foo.call(this);
  10. }
  11. var date = Date.prototype;
  12. date.toString = function(format){
  13. var year = this.getFullYear(),month = this.getMonth(),day = this.getDate();
  14. if(format){
  15. return format.replace(/yyyy/gi,year).replace(/yy/gi,year.toString().substr(2,2)).replace(/m/gi,month+1).replace(/d/gi,day);
  16. }else{return (year + "-" + (month + 1) + "-" + day);}
  17. }
  18. Date.diff = date.diff = function(lastDate,firstDate){
  19. if(!firstDate){firstDate = this;}
  20. var diff = {};
  21. diff.days = Math.floor((lastDate.getTime() - firstDate.getTime())/(1000*3600*24));
  22. diff.years = parseFloat(diff.days/365).toFixed(2);
  23. diff.months = parseFloat(diff.days/30).toFixed(2);
  24. diff.hours = diff.days*24;
  25. diff.seconds = diff.days*24*3600;
  26. diff.mins = diff.days*24*60;
  27. return diff;
  28. }
  29. Date.get = function(dateString){
  30. if(dateString){
  31. var re = /(\d{2,4}).+(\d{1,2}).+(\d{1,2})/g;
  32. var isCurrect = re.test(dateString);
  33. if(isCurrect){return new Date(RegExp.$1,RegExp.$2,RegExp.$3);}
  34. }
  35. return new Date();
  36. }
  37. var string = String.prototype;
  38. string.toNum = function(){return Number(this.replace(/\D/gi,""));}
  39. string.trim = function(isGlobal){
  40. if(isGlobal){return this.replace(/\s+/g,"");}
  41. else{return this.replace(/(^\s+)|(\s+$)/g,"");}
  42. }
  43. var array = Array.prototype;
  44. array.insert = function(index,element){
  45. this.splice(index,0,element);
  46. }
  47. array.remove = function(index){
  48. this.splice(index,1);
  49. }
  50. array.replace = function(index,element){
  51. this.splice(index,1,element);
  52. }
  53. array.include = function(element){
  54. var count = this.length;
  55. var i = 0;
  56. var indexes = [];
  57. while(i < count){
  58. if(this[i] == element){indexes.push(i);}i++;
  59. }
  60. return ((indexes.length < 1) ? -1 : indexes);
  61. }
  62. array.clone = function(cloner){
  63. this.clear();
  64. var count = cloner.length;
  65. for(var i=0;i<count;i++){
  66. this.push(cloner[i]);
  67. }
  68. }
  69. array.merger = function(target,index){
  70. index = (index ? index : 0);
  71. var count = target.length;
  72. for(var i=0;i<count;i++){
  73. this.insert((index+count),target[i]);
  74. }
  75. }
  76. array.each = function(foo){
  77. var count = this.length;
  78. var i = 0;
  79. while(i < count){
  80. foo(this[i],i,this);
  81. i++;
  82. }
  83. }
  84. array.filter = function(foo,isReverse){
  85. var count = this.length;
  86. var i = count - 1;
  87. while(i >= 0){
  88. var result = foo(this[i]);
  89. if(isReverse){
  90. if(result){
  91. this.remove(i);
  92. }
  93. }else{
  94. if(!result){
  95. this.remove(i);
  96. }
  97. }
  98. i--;
  99. }
  100. }
  101. array.distance = function(){
  102. var count = this.length;
  103. var i = count - 1;
  104. while(i >= 0){
  105. var indexes = this.include(this[i]);
  106. if(indexes.length && indexes.length > 1){
  107. this.remove(i);
  108. }
  109. i--;
  110. }
  111. }
  112. array.clear = function(){
  113. this.length = 0;
  114. }
  115. /******************** prototype end ********************/
  116. /******************** myjs ********************/
  117. var $ = function(obj,scoper){
  118. if(scoper){
  119. if(scoper.length && scoper.instance){
  120. var count = scoper.length;
  121. for(var i=0;i<count;i++){
  122. if(typeof obj == "string"){
  123. if(scoper[i].id){
  124. if(scoper[i].id.toLowerCase() == obj.toLowerCase()){obj = document.getElementById(obj);}
  125. }
  126. }else{
  127. if(scoper[i] == obj){obj = scoper[i];}
  128. }
  129. }
  130. }else{
  131. if(typeof obj == "string"){obj = scoper.getElementById(obj);}
  132. }
  133. }else{obj = document.getElementById(obj);}
  134. my.expand.call(obj);
  135. return obj;
  136. }
  137. var $n = function(name,scoper){
  138. var objs = [];
  139. if(scoper){
  140. if(scoper.length && scoper.instance){
  141. var count = scoper.length;
  142. for(var i=0;i<count;i++){
  143. if(scoper[i].name){
  144. if(scoper[i].name.toLowerCase() == name.toLowerCase()){objs.push(scoper[i]);}
  145. }
  146. }
  147. }else{objs = scoper.getElementsByName(name);}
  148. }else{objs = document.getElementsByName(name);}
  149. objs.instance = "array";
  150. my.expand.call(objs);
  151. for(var i=0;i<objs.length;i++){my.expand.call(objs[i]);}
  152. return objs;
  153. }
  154. var $t = function(tagName,scoper){
  155. var objs = [];
  156. if(scoper){
  157. if(scoper.length && scoper.instance){
  158. var count = scoper.length;
  159. for(var i=0;i<count;i++){
  160. if(scoper[i].tagName){
  161. if(scoper[i].tagName.toLowerCase() == tagName.toLowerCase()){objs.push(scoper[i]);}
  162. }
  163. }
  164. }else{objs = scoper.getElementsByTagName(tagName);}
  165. }else{objs = document.getElementsByTagName(tagName);}
  166. objs.instance = "array";
  167. my.expand.call(objs);
  168. for(var i=0;i<objs.length;i++){my.expand.call(objs[i]);}
  169. return objs;
  170. }
  171. var $css = function(cssName,scoper){
  172. var objs = [];
  173. var scope = scoper || document.body;
  174. if(scope.length && scope.instance){objs = scope;}
  175. else{objs = scope.childNodes;}
  176. var count = objs.length;
  177. var cObjs = [];
  178. for(var i=0;i<count;i++){
  179. if(objs[i].className){
  180. if(objs[i].className.toLowerCase() == cssName.toLowerCase() || (objs[i].className.toLowerCase().indexOf(cssName.toLowerCase()) != -1)){
  181. my.expand.call(objs[i]);
  182. cObjs.push(objs[i]);
  183. }
  184. }
  185. }
  186. cObjs.instance = "array";
  187. my.expand.call(cObjs);
  188. return cObjs;
  189. }
  190. var $c = function(tagName){
  191. tagName = tagName || "DIV";
  192. var obj = document.createElement(tagName);
  193. my.expand.call(obj);
  194. return obj;
  195. }
  196. var my = {
  197. vars : {
  198. methods : []
  199. },
  200. // 对象方法扩展
  201. expand : function(){
  202. this.$ = function(obj){return $(obj,this);}
  203. this.$t = function(tagName){return $t(tagName,this);}
  204. this.$css = function(cssName){return $css(cssName,this);}
  205. this.$n = function(name){return $n(name,this);}
  206. this.appendTo = function(render){render.appendChild(this);return this;}
  207. this.removeBy = function(render){render.remove(this);}
  208. this.remove = function(obj){
  209. if(obj.parentNode == this){
  210. this.removeChild(obj);
  211. }
  212. }
  213. this.html = function(html){
  214. if(html != null){this.innerHTML = html;}
  215. else{return this.innerHTML;}
  216. }
  217. this.text = function(text){
  218. if(text != null){this.innerText = text;}
  219. else{return this.innerText;}
  220. }
  221. this.parent = function(){
  222. if(this.parentNode){
  223. var node = this.parentNode
  224. my.expand.call(node);
  225. return node;
  226. }
  227. return null;
  228. }
  229. this.prev = function(){
  230. if(this.previousSibling){
  231. var node = this.previousSibling;
  232. my.expand.call(node);
  233. return node;
  234. }
  235. return null;
  236. }
  237. this.next = function(){
  238. if(this.nextSibling){
  239. var node = this.nextSibling;
  240. my.expand.call(node);
  241. return node;
  242. }
  243. return null;
  244. }
  245. this.bind = function(eventType,eventMethod,capture){my.events.bind(this,eventType,eventMethod,capture);}
  246. this.release = function(eventType,eventMethod,capture){my.events.release(this,eventType,eventMethod,capture);}
  247. this.attr = function(attrs){
  248. for(var key in attrs){this[key] = attrs[key];}
  249. return this;
  250. }
  251. this.css = function(sheet){
  252. if(typeof sheet == "string"){
  253. sheet.replace(/\{?\}?/,"");
  254. var shts = sheet.split(';');
  255. for(var i=0;i<shts.length-1;i++){
  256. var itm = shts[i].split(':');
  257. this.style[itm[0]] = itm[1];
  258. }
  259. }else{
  260. for(var key in sheet){this.style[key] = sheet[key];}
  261. }
  262. return this;
  263. }
  264. },
  265. /*
  266. 作用:检测浏览器
  267. 返回值:
  268. 各个浏览器信息组成的json对象
  269. */
  270. browser : function(){
  271. var navor = {};
  272. var agent = navigator.userAgent.toLowerCase();
  273. try{
  274. window.ActiveXObject ? navor.ie = agent.match(/msie ([\d.]+)/)[1] :
  275. document.getBoxObjectFor ? navor.firefox = agent.match(/firefox\/([\d.]+)/)[1] :
  276. window.MessageEvent && !document.getBoxObjectFor ? navor.chrome = agent.match(/chrome\/([\d.]+)/)[1] :
  277. window.opera ? navor.opera = agent.match(/opera.([\d.]+)/)[1] :
  278. window.openDatabase ? navor.safari = agent.match(/version\/([\d.]+)/)[1] : 0;
  279. }catch(e){}
  280. return navor;
  281. },
  282. /*
  283. 作用:检测操作系统(仅检测是否为windows操作系统)
  284. 返回值:
  285. 布尔值
  286. */
  287. system : function(){
  288. var isWindows = null;
  289. var plat = navigator.platform.toLowerCase();
  290. try{
  291. isWindows = ((plat.indexOf("win") != -1) ? true : false);
  292. }catch(e){}
  293. return isWindows;
  294. },
  295. /*
  296. 作用:页面加载事件 调用了vars中的loaded.methods变量
  297. 参数:
  298. @foo : 需要加载的事件的引用
  299. */
  300. loaded : function(foo){
  301. var methods = my.vars.methods;
  302. if((window.onload == null || window.onload == undefined) && methods.length < 1){
  303. my.events.bind(window,"load",function(){
  304. for(var i=0;i<methods.length;i++){
  305. try{methods[i]();}catch(e){throw e;}
  306. }
  307. });
  308. }
  309. methods.push(foo);
  310. },
  311. attach : {
  312. script : function(path,method){
  313. var heads = $t("head");
  314. var head = (heads.length) ? heads[0] : heads;
  315. var scriptObj = $c("script").attr({
  316. type : "text/javascript",
  317. charset : "utf-8"
  318. });
  319. var ajax = my.ajax;
  320. var loadScript = function(){
  321. if(ajax.args.xhr){
  322. if(ajax.args.xhr.readyState == 4){
  323. if(ajax.args.xhr.status == 200){// || ajax.args.xhr.status == 0
  324. scriptObj.text = ajax.args.xhr.responseText;
  325. scriptObj.appendTo(head);
  326. method();
  327. }
  328. }
  329. }
  330. }
  331. ajax.args.url = path;
  332. ajax.args.callback = loadScript;
  333. ajax.request();
  334. }
  335. },
  336. // 事件
  337. events : {
  338. /*
  339. 作用:绑定
  340. 参数:
  341. @target : 需要绑定事件的对象
  342. @eventType : 事件类型(如:click/mouseover等)
  343. @eventMethod : 事件处理函数的引用
  344. @capture : 是否全屏捕捉事件
  345. */
  346. bind : function(target,eventType,eventMethod,capture){
  347. eventType = eventType.replace(/on/gi,"");
  348. if(window.attachEvent){
  349. target.attachEvent("on" + eventType,eventMethod);
  350. }else if(window.addEventListener){
  351. target.addEventListener(eventType,eventMethod,capture || false);
  352. }else{target["on" + eventType] = eventMethod;}
  353. },
  354. /*
  355. 作用:释放
  356. 参数:
  357. 同bind函数
  358. */
  359. release : function(target,eventType,eventMethod,capture){
  360. eventType = eventType.replace(/on/gi,"");
  361. if(window.detachEvent){
  362. target.detachEvent("on" + eventType,eventMethod);
  363. }else if(window.removeEventListener){
  364. target.removeEventListener(eventType,eventMethod,capture || false);
  365. }else{target["on" + eventType] = null;}
  366. },
  367. /*
  368. 作用:获取
  369. 返回值:
  370. 经过格式化(仅为windows且ie条件下)的event对象
  371. */
  372. get : function(){
  373. var isMicrosoft = my.browser().ie && my.system();
  374. var e = window.event || my.events.get.caller.arguments[0];
  375. // windows下需格式化事件 以保证不同浏览器之间的用法一致
  376. if(isMicrosoft){
  377. e.charCode = ((e.type == "keypress") ? e.keyCode : 0);
  378. e.eventPhase = 2;
  379. e.isChar = (e.charCode > 0);
  380. e.pageX = e.clientX + document.body.scrollLeft;
  381. e.pageY = e.clientY + document.body.scrollTop;
  382. e.preventDefault = function(){this.returnValue = false;}
  383. if(e.type == "mouseover"){
  384. e.relateElement = e.fromElement;
  385. }else if(e.type == "mouseout"){
  386. e.relateElement = e.toElement;
  387. }
  388. e.stopPropagation = function(){this.cancleBubble = true;}
  389. e.target = e.srcElement;
  390. e.time = (new Date()).getTime();
  391. }
  392. return e;
  393. }
  394. },
  395. compatMode : function(){
  396. return ((document.compatMode.toLowerCase() == "css1compat") ? true : false);
  397. },
  398. /*
  399. 作用:得到指定对象的位置
  400. 参数:
  401. @target : 指定对象
  402. 返回值:
  403. 对象的left(x)和top(y)值
  404. */
  405. xy : function(target){
  406. var targetTop = target.offsetTop;
  407. var targetLeft = target.offsetLeft;
  408. while(target = target.offsetParent){
  409. targetTop += target.offsetTop;
  410. targetLeft += target.offsetLeft;
  411. }
  412. return {
  413. x : targetLeft,
  414. y : targetTop
  415. }
  416. },
  417. // 样式
  418. rules : {
  419. /*
  420. 作用:动态添加css样式
  421. 参数:
  422. @selector : css选择器名称(如body/.txtCss)
  423. @cssText : css样式
  424. @index : 索引(添加到当前样式的第几个位置)
  425. */
  426. add : function(selector,cssText,index){
  427. var heads = $t("head");
  428. var head = (heads.length) ? heads[0] : heads;
  429. var sheet = null;
  430. if(document.styleSheets.length < 1){
  431. $c("style").attr({type : "text/css"}).appendTo(head);
  432. }
  433. sheet = document.styleSheets[document.styleSheets.length - 1];
  434. if(sheet.addRule){
  435. sheet.addRule(selector,cssText,index);
  436. }else if(sheet.insertRule){
  437. sheet.insertRule(selector + "{" + cssText + "}",index);
  438. }
  439. },
  440. /*
  441. 作用:动态删除css样式
  442. 参数:
  443. @index : css样式的索引
  444. */
  445. remove : function(index){
  446. var sheet = null;
  447. if(document.styleSheets.length < 1){return false;}
  448. else{sheet = document.styleSheets[document.styleSheets.length - 1];}
  449. if(sheet.removeRule){sheet.removeRule(index);}
  450. else if(sheet.deleteRule){sheet.deleteRule(index);}
  451. },
  452. /*
  453. 作用:获取css样式
  454. 参数:
  455. @selector : css选择器名称
  456. @index : style的索引 文档中第几个style标签 默认为第一个
  457. 返回值:
  458. 若有selector 则返回单个样式对象
  459. 若无selector 则返回全部样式集合
  460. */
  461. get : function(selector,index){
  462. var sheet = document.styleSheets[document.styleSheets.length - 1];
  463. if(index){sheet = document.styleSheets[index];}
  464. var rules = (sheet.rules ? sheet.rules : sheet.cssRules);
  465. if(selector){
  466. for(var i=0;i<rules.length;i++){
  467. var rule = rules[i];
  468. if(rule.selectorText.toLowerCase() == selector.toLowerCase()){return rule;}
  469. }
  470. }else{return rules;}
  471. },
  472. css : function(path){
  473. var heads = $t("head");
  474. var head = (heads.length) ? heads[0] : heads;
  475. return $c("style").attr({type : "text/css",href:path}).appendTo(head);
  476. }
  477. },
  478. // cookies
  479. cookies : function(key,value,expires,domain,path,secure){
  480. this.key = null;
  481. this.value = null;
  482. this.expires = null;
  483. this.domain = null;
  484. this.path = null;
  485. this.secure = null;
  486. this.cook = "";
  487. this.create = function(k,v,e,d,p,s){
  488. if(this.key == null && this.value == null){
  489. if(!(k && v)){
  490. return false; // already set
  491. }else{
  492. this.set(k,v,e,d,p,s);
  493. }
  494. }
  495. this.cook = this.key + "=" + escape(this.value);
  496. if(this.expires){this.cook += ";expires=" + this.expires.toGMTString();}
  497. if(this.domain){this.cook += ";domain=" + this.domain;}
  498. if(this.path){this.cook += ";path=" + this.path;}
  499. if(this.secure){this.cook += ";" + this.secure;}
  500. document.cookie = this.cook;
  501. }
  502. this.set = function(k,v,e,d,p,s){
  503. this.key = null || k;
  504. this.value = null || v;
  505. this.expires = null || e;
  506. this.domain = null || d;
  507. this.path = null || p;
  508. this.secure = null || s;
  509. }
  510. this.get = function(key){
  511. var cookie = document.cookie;
  512. var key = key || this.key;
  513. if(cookie.length > 0){
  514. var c_start = cookie.indexOf(key + "=");
  515. if(c_start != -1){
  516. c_start = c_start + key.length + 1;
  517. c_end = cookie.indexOf(";",c_start);
  518. if(c_end == -1){c_end = cookie.length;}
  519. return unescape(cookie.substring(c_start,c_end));
  520. }
  521. }
  522. return null;
  523. }
  524. this.remove = function(key){
  525. var key = key || this.key;
  526. var expires = Date.get();
  527. expires.setTime(expires.getTime() - (expires.getTime() + 1));
  528. document.cookie = key + "=my;expires=" + expires.toGMTString();
  529. if((!key) || (this.key == key)){this.clear();}
  530. }
  531. this.clear = function(){
  532. this.key = null;
  533. this.value = null;
  534. this.expires = null;
  535. this.domain = null;
  536. this.path = null;
  537. this.secure = null;
  538. this.cook = "";
  539. }
  540. this.isExist = function(key){
  541. var value = this.get(key);
  542. if(value != null && value != ""){return value;}
  543. return false;
  544. }
  545. this.set(key,value,expires,domain,path,secure);
  546. if(this.key != null && this.value != null){
  547. this.create();
  548. }
  549. },
  550. // another expand
  551. // extend : function(namespace,json){
  552. // // = fn;
  553. // var cls = my[namespace] = {};
  554. // for(var key in json){
  555. // cls[key] = json[key];
  556. // }
  557. // },
  558. ajax : {
  559. args : {
  560. xhr : null,
  561. url : null,
  562. analy : true,
  563. sendType : "GET",
  564. sendData : null,
  565. callback : null
  566. },
  567. request : function(){
  568. try{my.ajax.args.xhr = new ActiveXObject("Msxml2.XMLHTTP");}
  569. catch(e){
  570. try{my.ajax.args.xhr = new ActiveXObject("Microsoft.XMLHTTP");}
  571. catch(e){
  572. try{my.ajax.args.xhr = new XMLHttpRequest();}
  573. catch(e){my.ajax.args.xhr = null;}
  574. }
  575. }
  576. if(!my.ajax.args.xhr){return false;}
  577. else{
  578. my.ajax.args.xhr.open(my.ajax.args.sendType,my.ajax.args.url,my.ajax.args.analy);
  579. // header
  580. my.ajax.args.xhr.onreadystatechange = my.ajax.args.callback;
  581. my.ajax.args.xhr.send(my.ajax.args.sendData);
  582. //return my.ajax.args.xhr;
  583. }
  584. }/*,
  585. receive : function(){
  586. if(my.ajax.args.xhr){
  587. if(my.ajax.args.xhr.readyState == 4){
  588. if(my.ajax.args.xhr.status == 200){
  589. return true;
  590. }
  591. }
  592. }
  593. },*/
  594. }
  595. }
  596. window.my = window.M = my;
  597. /******************** myjs end ********************/