收藏 分销(赏)

32个可以直接拿来使用的js.doc

上传人:二*** 文档编号:4748466 上传时间:2024-10-11 格式:DOC 页数:11 大小:52KB
下载 相关 举报
32个可以直接拿来使用的js.doc_第1页
第1页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、1、原生JavaScript实现字符串长度截取function cutstr(str, len) var temp; var icount = 0; var patrn = /x00-xff/; var strre = ; for (var i = 0; i str.length; i+) if (icount len - 1) temp = str.substr(i, 1); if (patrn.exec(temp) = null) icount = icount + 1 else icount = icount + 2 strre += temp else break return str

2、re + .2、原生JavaScript获取域名主机function getHost(url) var host = null; if(typeof url = undefined| null = url) url = window.location.href; var regex = /w+:/(/*).*/; var match = url.match(regex); if(typeof match != undefined & null != match) host = match1; return host;3、原生JavaScript清除空格String.prototype.trim

3、 = function() var reExtraSpace = /s*(.*?)s+$/; return this.replace(reExtraSpace, $1)4、原生JavaScript替换全部String.prototype.replaceAll = function(s1, s2) return this.replace(new RegExp(s1, gm), s2) 5、原生JavaScript转义html标签function HtmlEncode(text) return text.replace(/&/g, &).replace(/g, ").replace

4、(/g, >) 6、原生JavaScript还原html标签function HtmlDecode(text) return text.replace(/&/g, &).replace(/"/g, ).replace(/</g, ) 7、原生JavaScript时间日期格式转换Date.prototype.Format = function(formatStr) var str = formatStr; var Week = 日, 一, 二, 三, 四, 五, 六; str = str.replace(/yyyy|YYYY/, this.getFullYear()

5、; str = str.replace(/yy|YY/, (this.getYear() % 100) 9 ? (this.getYear() % 100).toString() : 0 + (this.getYear() % 100); str = str.replace(/MM/, (this.getMonth() + 1) 9 ? (this.getMonth() + 1).toString() : 0 + (this.getMonth() + 1); str = str.replace(/M/g, (this.getMonth() + 1); str = str.replace(/w|

6、W/g, Weekthis.getDay(); str = str.replace(/dd|DD/, this.getDate() 9 ? this.getDate().toString() : 0 + this.getDate(); str = str.replace(/d|D/g, this.getDate(); str = str.replace(/hh|HH/, this.getHours() 9 ? this.getHours().toString() : 0 + this.getHours(); str = str.replace(/h|H/g, this.getHours();

7、str = str.replace(/mm/, this.getMinutes() 9 ? this.getMinutes().toString() : 0 + this.getMinutes(); str = str.replace(/m/g, this.getMinutes(); str = str.replace(/ss|SS/, this.getSeconds() 9 ? this.getSeconds().toString() : 0 + this.getSeconds(); str = str.replace(/s|S/g, this.getSeconds(); return st

8、r8、原生JavaScript判断是否为数字类型function isDigit(value) var patrn = /0-9*$/; if (patrn.exec(value) = null | value = ) return false else return true 9、原生JavaScript设置cookie值function setCookie(name, value, Hours) var d = new Date(); var offset = 8; var utc = d.getTime() + (d.getTimezoneOffset() * 60000); var n

9、d = utc + ( * offset); var exp = new Date(nd); exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); document.cookie = name + = + escape(value) + ;path=/;expires= + exp.toGMTString() + ;domain=;10、原生JavaScript获取cookie值function getCookie(name) var arr = document.cookie.match(new RegExp(| ) + name + =(

10、;*)(;|$); if (arr != null) return unescape(arr2); return null11、原生JavaScript加入收藏夹 function AddFavorite(sURL, sTitle) try window.external.addFavorite(sURL, sTitle) catch(e) try window.sidebar.addPanel(sTitle, sURL, ) catch(e) alert(加入收藏失败,请使用Ctrl+D进行添加) 12、原生JavaScript设为首页function setHomepage() if (d

11、ocument.all) document.body.style.behavior = url(#default#homepage); document.body.setHomePage(http:/*) else if (window.sidebar) if (scape) try netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect) catch(e) alert(该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_p

12、rincipal_support 值该为true) var prefs = Components.classesmozilla.org/preferences-service;1.getService(Components.interfaces.nsIPrefBranch); prefs.setCharPref(browser.startup.homepage, http:/*) 13、原生JavaScript判断IE6var ua = navigator.userAgent.toLowerCase();var isIE6 = ua.indexOf(msie 6) -1;if (isIE6)

13、try document.execCommand(BackgroundImageCache, false, true) catch(e) 14、原生JavaScript加载样式文件function LoadStyle(url) try document.createStyleSheet(url) catch(e) var cssLink = document.createElement(link); cssLink.rel = stylesheet; cssLink.type = text/css; cssLink.href = url; var head = document.getElem

14、entsByTagName(head)0; head.appendChild(cssLink) 15、原生JavaScript返回脚本内容function evalscript(s) if(s.indexOf(script) = -1) return s; var p = /*?(x00*?)/ig; var arr = ; while(arr = p.exec(s) var p1 = /*?src=(*?)*?(reload=1)?(?:charset=(w-+?)?/i; var arr1 = ; arr1 = p1.exec(arr0); if(arr1) appendscript(ar

15、r11, , arr12, arr13); else p1 = /(x00+?)/i; arr1 = p1.exec(arr0); appendscript(, arr12, arr11.indexOf(reload=) != -1); return s;16、原生JavaScript清除脚本内容function stripscript(s) return s.replace(/.*?/ig, ); 17、原生JavaScript动态加载脚本文件function appendscript(src, text, reload, charset) var id = hash(src + text)

16、; if(!reload & in_array(id, evalscripts) return; if(reload & $(id) $(id).parentNode.removeChild($(id); evalscripts.push(id); var scriptNode = document.createElement(script); scriptNode.type = text/javascript; scriptNode.id = id; scriptNode.charset = charset ? charset : (BROWSER.firefox ? document.ch

17、aracterSet : document.charset); try if(src) scriptNode.src = src; scriptNode.onloadDone = false; scriptNode.onload = function () scriptNode.onloadDone = true; JSLOADEDsrc = 1; ; scriptNode.onreadystatechange = function () if(scriptNode.readyState = loaded | scriptNode.readyState = complete) & !scrip

18、tNode.onloadDone) scriptNode.onloadDone = true; JSLOADEDsrc = 1; ; else if(text) scriptNode.text = text; document.getElementsByTagName(head)0.appendChild(scriptNode); catch(e) 18、原生JavaScript返回按ID检索的元素对象 function $(id) return !id ? null : document.getElementById(id); 19、原生JavaScript返回浏览器版本内容function

19、 browserVersion(types) var other = 1; for(i in types) var v = typesi ? typesi : i; if(USERAGENT.indexOf(v) != -1) var re = new RegExp(v + (/|s)(d.+), ig); var matches = re.exec(USERAGENT); var ver = matches != null ? matches2 : 0; other = ver != 0 & v != mozilla ? 0 : other; else var ver = 0; eval(B

20、ROWSER. + i + = ver); BROWSER.other = other;20、原生JavaScript元素显示的通用方法function $(id) return !id ? null : document.getElementById(id);function display(id) var obj = $(id); if(obj.style.visibility) obj.style.visibility = obj.style.visibility = visible ? hidden : visible; else obj.style.display = obj.sty

21、le.display = ? none : ; 21、原生JavaScript中有insertBefore方法,可惜却没有insertAfter方法?用如下函数实现function insertAfter(newChild,refChild) var parElem=refChild.parentNode; if(parElem.lastChild=refChild) refChild.appendChild(newChild); else parElem.insertBefore(newChild,refChild.nextSibling); 22、原生JavaScript中兼容浏览器绑定元

22、素事件function addEventSamp(obj,evt,fn) if (obj.addEventListener) obj.addEventListener(evt, fn, false); else if(obj.attachEvent) obj.attachEvent(on+evt,fn); 23、原生JavaScript光标停在文字的后面,文本框获得焦点时调用function focusLast() var e = event.srcElement; var r =e.createTextRange(); r.moveStart(character,e.value.length

23、); r.collapse(true); r.select(); 24、原生JavaScript检验URL链接是否有效function getUrlState(URL) var xmlhttp = new ActiveXObject(microsoft.xmlhttp); xmlhttp.Open(GET,URL, false); try xmlhttp.Send(); catch(e) finally var result = xmlhttp.responseText; if(result) if(xmlhttp.Status=200) return(true); else return(f

24、alse); else return(false); 25、原生JavaScript格式化CSS样式代码function formatCss(s)/格式化代码 s = s.replace(/s*(:;,)s*/g, $1); s = s.replace(/;s*;/g, ;); /清除连续分号 s = s.replace(/,s.#d*/g, ); s = s.replace(/(s)(s)/g, $1 nt$2); s = s.replace(/(s)(n*)/g, $1nn$2); s = s.replace(/(s);(s)/g, $1;nt$2); return s;26、原生Java

25、Script压缩CSS样式代码function yasuoCss (s) /压缩代码 s = s.replace(/*(.|n)*?*/g, ); /删除注释 s = s.replace(/s*(:;,)s*/g, $1); s = s.replace(/,s.#d*/g, ); /容错处理 s = s.replace(/;s*;/g, ;); /清除连续分号 s = s.match(/s*(S+(s+S+)*)s*$/); /去掉首尾空白 return (s = null) ? : s1;27、原生JavaScript获取当前路径var currentPageUrl = ;if (typeo

26、f this.href = undefined) currentPageUrl = document.location.toString().toLowerCase();else currentPageUrl = this.href.toString().toLowerCase();28、原生JavaScriptIP转成整型function _ip2int(ip) var num = 0; ip = ip.split(.); num = Number(ip0) * 256 * 256 * 256 + Number(ip1) * 256 * 256 + Number(ip2) * 256 + N

27、umber(ip3); num = num 0; return num;29、原生JavaScript整型解析为IP地址function _int2iP(num) var str; var tt = new Array(); tt0 = (num 24) 0; tt1 = (num 24) 0; tt2 = (num 24; tt3 = (num 24; str = String(tt0) + . + String(tt1) + . + String(tt2) + . + String(tt3); return str;30、原生JavaScript实现checkbox全选与全不选functi

28、on checkAll() var selectall = document.getElementById(selectall); var allbox = document.getElementsByName(allbox); if (selectall.checked) for (var i = 0; i allbox.length; i+) allboxi.checked = true; else for (var i = 0; i allbox.length; i+) allboxi.checked = false; 31、原生JavaScript判断是否移动设备function is

29、Mobile() if (typeof this._isMobile = boolean) return this._isMobile; var screenWidth = this.getScreenWidth(); var fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport | rendererModel.runningExperiments.fixviewport; var fixViewPortsExperimentRunning = fixViewPortsExperiment & (fixVie

30、wPortsExperiment.toLowerCase() = new); if(!fixViewPortsExperiment) if(!this.isAppleMobileDevice() screenWidth = screenWidth/window.devicePixelRatio; var isMobileScreenSize = screenWidth 600; var isMobileUserAgent = false; this._isMobile = isMobileScreenSize & this.isTouchScreen(); return this._isMobile;32、原生JavaScript判断是否移动设备访问 function isMobileUserAgent() return (/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase();

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 环境建筑 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服