日常收集整理的JavaScript常用函数方法(2)

functioappendscript(srctextreloadcharset{ vaihash(srtext); if(!reloa&in_array(idevalscripts)return; if(reloa&$(id){ $(id).parentNode.removeChild($(id)); } evalscripts.push(id); vascriptNoddocument.createElement("script"); scriptNode.typ"text/javascript"; scriptNode.iid; scriptNode.charsecharsecharse(BROWSER.firefodocument.characterSedocument.charset); tr{ if(src{ scriptNode.srsrc; scriptNode.onloadDonfalse; scriptNode.onloafunctio({ scriptNode.onloadDontrue; JSLOADED[src; }; scriptNode.onreadystatechangfunctio({ if((scriptNode.readyStat='loaded|scriptNode.readyStat='complete'&!scriptNode.onloadDone{ scriptNode.onloadDontrue; JSLOADED[src; } }; elsif(text){ scriptNode.textext; } document.getElementsByTagName('head')[].appendChild(scriptNode); catch(e{} }

返回按ID检索的元素对象

functio$(id{ retur!inuldocument.getElementById(id); }

跨浏览器绑定事件

functioaddEventSamp(obj,evt,fn){ if(!oTarget){return;} i(obj.addEventListener{ obj.addEventListener(evtfnfalse); }elsif(obj.attachEvent){ obj.attachEvent('on'+evt,fn); }else{ oTarget["onsEvtTypefn; } }

跨浏览器删除事件

functiodelEvt(obj,evt,fn){ if(!obj){return;} if(obj.addEventListener){ obj.addEventListener(evt,fn,false); }elsif(oTarget.attachEvent){ obj.attachEvent("onevt,fn); }else{ obj["onevtfn; } }

为元素添加on方法

Element.prototype.oElement.prototype.addEventListener; NodeList.prototype.ofunctio(eventfn{、 []['forEach'].call(thisfunctio(el{ el.on(eventfn); }); returthis; };

为元素添加trigger方法

Element.prototype.triggefunctio(typedata{ vaevendocument.createEvent('HTMLEvents'); event.initEvent(typetruetrue); event.datdat|{}; event.eventNamtype; event.targethis; this.dispatchEvent(event); returthis; }; NodeList.prototype.triggefunctio(event{ []['forEach'].call(thisfunctio(el{ el['trigger'](event); }); returthis; };

检验URL链接是否有效

functiogetUrlState(URL){ vaxmlhttneActiveXObject("microsoft.xmlhttp"); xmlhttp.Open("GET",URLfalse); try{ xmlhttp.Send(); }catch(e){ }finally{ varesulxmlhttp.responseText; if(result){ if(xmlhttp.Status==){ return(true); }else{ return(false); } }else{ return(false); } } }

格式化CSS样式代码

.functioformatCss(s){//格式化代码 s.replace(/\s*([\{\}\:\;\,])\s*/g"$"); s.replace(/;\s*;/g";")//清除连续分号 s.replace(/\,[\s\.\#\d]*{/g"{"); s.replace(/([^\s])\{([^\s])/g"${\n\t$"); s.replace(/([^\s])\}([^\n]*)/g"$\n}\n$"); s.replace(/([^\s]);([^\s\}])/g"$;\n\t$"); returs; }

压缩CSS样式代码

functiocompressCs(s{//压缩代码 s.replace(/\/\*(.|\n)*?\*\//g"")//删除注释 s.replace(/\s*([\{\}\:\;\,])\s*/g"$"); s.replace(/\,[\s\.\#\d]*\{/g"{")//容错处理 s.replace(/;\s*;/g";")//清除连续分号 s.match(/^\s*(\S+(\s+\S+)*)\s*$/)//去掉首尾空白 retur(=null"s[]; }

获取当前路径

vacurrentPageUr""; i(typeothis.hre=="undefined"{ currentPageUrdocument.location.toString().toLowerCase(); }els{ currentPageUrthis.href.toString().toLowerCase(); }

判断是否移动设备

functioisMobile(){ i(typeothis._isMobil=='boolean'){ returthis._isMobile; } vascreenWidtthis.getScreenWidth(); vafixViewPortsExperimenrendererModel.runningExperiments.FixViewpor||rendererModel.runningExperiments.fixviewport; vafixViewPortsExperimentRunninfixViewPortsExperimen&(fixViewPortsExperiment.toLowerCase(=="new"); if(!fixViewPortsExperiment){ if(!this.isAppleMobileDevice()){ screenWidtscreenWidth/window.devicePixelRatio; } } vaisMobileScreenSizscreenWidt; vaisMobileUserAgenfalse; this._isMobilisMobileScreenSiz&this.isTouchScreen(); returthis._isMobile; }

判断是否移动设备访问

functioisMobileUserAgent(){ retur(/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase())); }

判断是否苹果移动设备访问

functioisAppleMobileDevice(){ retur(/iphone|ipod|ipad|Macintosh/i.test(navigator.userAgent.toLowerCase())); }

判断是否安卓移动设备访问

functioisAndroidMobileDevice(){ retur(/android/i.test(navigator.userAgent.toLowerCase())); }

判断是否Touch屏幕

functioisTouchScreen(){ retur(('ontouchstartiwindow|window.DocumentTouc&documeninstanceoDocumentTouch); }

判断是否打开视窗

functioisViewportOpen({ retur!!document.getElementById('wixMobileViewport'); }

获取移动设备初始化大小

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzypx.html