内含95个工具函数 前端大佬都在使用的JavaScript工具函数宝典( 四 )

35 检验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 false;}} else {return false;}}}36 获取URL上的参数// 获取URL中的某参数值,不区分大小写// 获取URL中的某参数值,不区分大小写,// 默认是取'hash'里的参数,// 如果传其他参数支持取‘search’中的参数// @param {String} name 参数名称export function getUrlParam(name, type = "hash") {let newName = name,reg = new RegExp("(^|&)" + newName + "=([^&]*)(&|$)", "i"),paramHash = window.location.hash.split("?")[1] || "",paramSearch = window.location.search.split("?")[1] || "",param;type === "hash" ? (param = paramHash) : (param = paramSearch);let result = param.match(reg);if (result != null) {return result[2].split("/")[0];}return null;}37 获取窗体可见范围的宽与高function getViewSize() {var de = document.documentElement;var db = document.body;var viewW = de.clientWidth == 0 ? db.clientWidth : de.clientWidth;var viewH = de.clientHeight == 0 ? db.clientHeight : de.clientHeight;return Array(viewW, viewH);}38 判断是否安卓移动设备访问function isAndroidMobileDevice() {return /android/i.test(navigator.userAgent.toLowerCase());}39 判断是否苹果移动设备访问function isAppleMobileDevice() {return /iphone|ipod|ipad|macintosh/i.test(navigator.userAgent.toLowerCase());}40 是否是某类手机型号// 用devicePixelRatio和分辨率判断const isIphonex = () => {// X XS, XS Max, XRconst xSeriesConfig = [{devicePixelRatio: 3,width: 375,height: 812},{devicePixelRatio: 3,width: 414,height: 896},{devicePixelRatio: 2,width: 414,height: 896}];// h5if (typeof window !== "undefined" && window) {const isIOS = /iphone/gi.test(window.navigator.userAgent);if (!isIOS) return false;const { devicePixelRatio, screen } = window;const { width, height } = screen;return xSeriesConfig.some(item =>item.devicePixelRatio === devicePixelRatio &&item.width === width &&item.height === height);}return false;};41 判断是否是移动设备访问function isMobileUserAgent() {return /iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase());}42 判断是否移动设备function isMobile() {if (typeof this._isMobile === "boolean") {return this._isMobile;}var screenWidth = this.getScreenWidth();var fixViewPortsExperiment =rendererModel.runningExperiments.FixViewport ||rendererModel.runningExperiments.fixviewport;var fixViewPortsExperimentRunning =fixViewPortsExperiment && fixViewPortsExperiment.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;}43 判断是否手机号码function isMobileNumber(e) {var i ="134,135,136,137,138,139,150,151,152,157,158,159,187,188,147,182,183,184,178",n = "130,131,132,155,156,185,186,145,176",a = "133,153,180,181,189,177,173,170",o = e || "",r = o.substring(0, 3),d = o.substring(0, 4),s =!!/^1d{10}$/.test(o) &&(n.indexOf(r) >= 0? "联通": a.indexOf(r) >= 0? "电信": "1349" == d? "电信": i.indexOf(r) >= 0? "移动": "未知");return s;}44 判断鼠标是否移出事件function isMouseout(e, handler) {if (e.type !== "mouseout") {return false;}var reltg = e.relatedTarget? e.relatedTarget: e.type === "mouseout"? e.toElement: e.fromElement;while (reltg && reltg !== handler) {reltg = reltg.parentNode;}return reltg !== handler;}45 判断是否Touch屏幕function isTouchScreen() {return ("ontouchstart" in window ||(window.DocumentTouch && document instanceof DocumentTouch));}46 判断是否为网址function isURL(strUrl) {var regular = /^b(((https?|ftp)://)?[-a-z0-9]+(.[-a-z0-9]+)*.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]d)|(1dd)|([1-9]d)|d))b(/[-a-z0-9_:@&?=+,.!/~%$]*)?)$/i;if (regular.test(strUrl)) {return true;} else {return false;}}47 判断是否打开视窗function isViewportOpen() {return !!document.getElementById("wixMobileViewport");}48 加载样式文件function loadStyle(url) {try {document.createStyleSheet(url);} catch (e) {var cssLink = document.createElement("link");cssLink.rel = "stylesheet";cssLink.type = "text/css";cssLink.href = https://www.isolves.com/it/cxkf/yy/js/2020-08-05/url;var head = document.getElementsByTagName("head")[0];head.appendChild(cssLink);}}


推荐阅读