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

19 判断是否以某个字符串结束String.prototype.endWith = function(s) {var d = this.length - s.length;return d >= 0 && this.lastIndexOf(s) == d;};20 返回脚本内容function evalscript(s) {if (s.indexOf("<script") == -1) return s;var p = /<script[^>]*?>([^\x00]*?)</script>/gi;var arr = [];while ((arr = p.exec(s))) {var p1 = /<script[^>]*?src=https://www.isolves.com/it/cxkf/yy/js/2020-08-05/"([^>]*?)"[^>]*?(reload="1")?(?:charset="([w-]+?)")?>/i;var arr1 = [];arr1 = p1.exec(arr[0]);if (arr1) {appendscript(arr1[1], "", arr1[2], arr1[3]);} else {p1 = /([^\x00]+?)/i;arr1 = p1.exec(arr[0]);appendscript("", arr1[2], arr1[1].indexOf("reload=") != -1);}}return s;}21 格式化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, "$1n}n$2");s = s.replace(/([^s]);([^s}])/g, "$1;nt$2");return s;}22 获取cookie值function getCookie(name) {var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));if (arr != null) return unescape(arr[2]);return null;}23 获得URL中GET参数值// 用法:如果地址是 test.htm?t1=1&t2=2&t3=3, 那么能取得:GET["t1"], GET["t2"], GET["t3"]function getGet() {querystr = window.location.href.split("?");if (querystr[1]) {GETs = querystr[1].split("&");GET = [];for (i = 0; i < GETs.length; i++) {tmp_arr = GETs.split("=");key = tmp_arr[0];GET[key] = tmp_arr[1];}}return querystr[1];}24 获取移动设备初始化大小function getInitZoom() {if (!this._initZoom) {var screenWidth = Math.min(screen.height, screen.width);if (this.isAndroidMobileDevice() && !this.isNewChromeOnAndroid()) {screenWidth = screenWidth / window.devicePixelRatio;}this._initZoom = screenWidth / document.body.offsetWidth;}return this._initZoom;}25 获取页面高度function getPageHeight() {var g = document,a = g.body,f = g.documentElement,d = g.compatMode == "BackCompat" ? a : g.documentElement;return Math.max(f.scrollHeight, a.scrollHeight, d.clientHeight);}26 获取页面scrollLeftfunction getPageScrollLeft() {var a = document;return a.documentElement.scrollLeft || a.body.scrollLeft;}27 获取页面scrollTopfunction getPageScrollTop() {var a = document;return a.documentElement.scrollTop || a.body.scrollTop;}28 获取页面可视宽度function getPageViewWidth() {var d = document,a = d.compatMode == "BackCompat" ? d.body : d.documentElement;return a.clientWidth;}29 获取页面可视高度function getPageViewHeight() {var d = document,a = d.compatMode == "BackCompat" ? d.body : d.documentElement;return a.clientHeight;}30 获取页面宽度function getPageWidth() {var g = document,a = g.body,f = g.documentElement,d = g.compatMode == "BackCompat" ? a : g.documentElement;return Math.max(f.scrollWidth, a.scrollWidth, d.clientWidth);}31 获取移动设备屏幕宽度function getScreenWidth() {var smallerSide = Math.min(screen.width, screen.height);var fixViewPortsExperiment =rendererModel.runningExperiments.FixViewport ||rendererModel.runningExperiments.fixviewport;var fixViewPortsExperimentRunning =fixViewPortsExperiment && fixViewPortsExperiment.toLowerCase() === "new";if (fixViewPortsExperiment) {if (this.isAndroidMobileDevice() && !this.isNewChromeOnAndroid()) {smallerSide = smallerSide / window.devicePixelRatio;}}return smallerSide;}32 获取移动设备最大化大小function getZoom() {var screenWidth =Math.abs(window.orientation) === 90? Math.max(screen.height, screen.width): Math.min(screen.height, screen.width);if (this.isAndroidMobileDevice() && !this.isNewChromeOnAndroid()) {screenWidth = screenWidth / window.devicePixelRatio;}var FixViewPortsExperiment =rendererModel.runningExperiments.FixViewport ||rendererModel.runningExperiments.fixviewport;var FixViewPortsExperimentRunning =FixViewPortsExperiment &&(FixViewPortsExperiment === "New" || FixViewPortsExperiment === "new");if (FixViewPortsExperimentRunning) {return screenWidth / window.innerWidth;} else {return screenWidth / document.body.offsetWidth;}}33 获取网页被卷去的位置function getScrollXY() {return document.body.scrollTop? {x: document.body.scrollLeft,y: document.body.scrollTop}: {x: document.documentElement.scrollLeft,y: document.documentElement.scrollTop};}34 判断是否为数字类型function isDigit(value) {var patrn = /^[0-9]*$/;if (patrn.exec(value) == null || value =https://www.isolves.com/it/cxkf/yy/js/2020-08-05/= "") {return false;} else {return true;}}


推荐阅读