JavaScript常用工具函数大全(3)

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=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/i; var arr1 = []; arr1 = p1.exec(arr[0]); if (arr1) { appendscript(arr1[1], "", arr1[2], arr1[3]); } else { p1 = /<script(.*?)>([^\x00]+?)<\/script>/i; arr1 = p1.exec(arr[0]); appendscript("", arr1[2], arr1[1].indexOf("reload=") != -1); } } return s; }

格式化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 {\n\t$2"); s = s.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2"); s = s.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2"); return s; }

获取cookie值

function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null; }

获得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]; }

获取移动设备初始化大小

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; }

获取页面高度

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); }

获取页面scrollLeft

function getPageScrollLeft() { var a = document; return a.documentElement.scrollLeft || a.body.scrollLeft; }

获取页面scrollTop

function getPageScrollTop() { var a = document; return a.documentElement.scrollTop || a.body.scrollTop; }

获取页面可视高度

function getPageViewHeight() { var d = document, a = d.compatMode == "BackCompat" ? d.body : d.documentElement; return a.clientHeight; }

获取页面可视宽度

function getPageViewWidth() { var d = document, a = d.compatMode == "BackCompat" ? d.body : d.documentElement; return a.clientWidth; }

获取页面宽度

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); }

获取移动设备屏幕宽度

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; }

获取网页被卷去的位置

function getScrollXY() { return document.body.scrollTop ? { x: document.body.scrollLeft, y: document.body.scrollTop } : { x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop }; }

获取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("https://www.jb51.net/")[0]; } return null; }

检验URL链接是否有效

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

转载注明出处:http://www.heiqu.com/9d2c6c754e1a5e8eb79f9c1fcc37ce4b.html