$g.$utils.实用工具类

简介: $g.$utils.实用工具类

$g.$utils = {

   /**舒工Ajax-lite 1.0 -- 最精简的ajax自定义访问方法*/

   ajax: function (o) {

       var p = o.post, g = o.get, d = p.data, a = p.async, J = 'json', j = p[J], s = g.success, e = g.error;

       d = {

           async: a == undefined || a, /* false(解决手机端同步多次请求接口报错的问题)*/ timeout: 60000, type: p.type || "post", url: p.url, data: d, success: function (r) {

               s && s(r);

           }, error: function (r) {

               e && e(r);

           },

       };

       j ? (d.data = JSON.stringify(d.data), d.contentType = 'application/' + J) : (d.dataType = J), $.ajax(d);

   },

   hash: {

       get: function (win) {

           return decodeURIComponent((win || top).location.hash.substr(1));

       }, getSearchAndHash: function (win) {

           win || (win = top);

           return decodeURIComponent(win.location.search + win.location.hash);

       }, set: function (hash, win, isReload) {

           hash || (hash = "");

           win = win || top;

           win.location.hash = hash;

           isReload && win.location.reload();

       }, addListener: function (win) {

           (win || window).onhashchange = function () {

               this.location.reload();

           }

       }, getQueryString: function (name, win, isEncode) {

           var r = (win || top).location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));

           if (r != null) {

               return isEncode ? r[2] : decodeURIComponent(r[2]);

           }

           return null;

       }, parseQuery: function (url) {

           var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g, ls = url || location.search.substr(1), obj = {};

           while (reg.exec(ls)) {

               obj[RegExp.$1] = decodeURIComponent(RegExp.$2);

           }

           return obj;

       }, getToken: function () {

           return this.getQueryString("token")

       }, setUrlSearch: function (search, win, isReload) {  /**修改浏览器地址“?”后面(含?)的内容*/

           /*此方法可以点击网页返回按钮*/

           win = win || top;

           win.history.pushState(null, null, search || "./");

           isReload && win.location.reload();

       }, replceUrlSearch: function (search, win, isReload) { /*此方法无网页返回按钮行为*/

           win = win || top;

           win.history.replaceState(null, null, search || "./");

           isReload && win.location.reload();

       }, getFileName: function () {

           var lp = location.pathname, fn = lp.substr(lp.lastIndexOf("/") + 1);

           return fn.substr(0, fn.lastIndexOf("."));

       }

   },

   /**绑定数据神器*/ bind: {

       set: function (h, l, v) {

           if (!h) return console.log("html传参为空", l, v);

           return h.replace(new RegExp("{" + l + "}", "g"), v).replace(new RegExp("\\[" + l + "\\]", "g"), v);

       }, object: function (h, o) {

           for (var a in o) var b = o[a], h = this.set(h, a, b);

           return h;

       }, a: function (html, obj) {

           return this.object(html, obj);

       }

   },

   /**去掉html标签(真正意义上去掉所有html标签包括内嵌的css样式)*/

   string: {

       stripHTML: function (str, isRemoveNewLine) {

           var t = document.createElement("div");

           t.innerHTML = str;

           document.querySelector("html").appendChild(t);

           var r = t.innerText;

           t.parentNode.removeChild(t);

           isRemoveNewLine && (r = r.replace(/[\r\n]/g, ""));

           return r;

       },

   },

   screen: {

 

       // 在全屏与非全屏之间来回切换

       toggleFullScreen(d) {

           this.isFullScreen() ? this.exitFullScreen() : this.fullScreen();

       },

 

       /**实现F11全屏效果*/  

       fullScreen: function () {

           var docElm = document.documentElement;

           /*W3C*/

           if (docElm.requestFullscreen) {

               docElm.requestFullscreen();

           }/*FireFox */ else if (docElm.mozRequestFullScreen) {

               docElm.mozRequestFullScreen();

           }/*Chrome等 */ else if (docElm.webkitRequestFullScreen) {

               docElm.webkitRequestFullScreen();

           }/*IE11*/ else if (docElm.msRequestFullscreen) {

               docElm.msRequestFullscreen();

           }

       },

       /**退出F11全屏*/  

       exitFullScreen: function () {

           if (document.exitFullscreen) {

               document.exitFullscreen();

           } else if (document.mozCancelFullScreen) {

               document.mozCancelFullScreen();

           } else if (document.webkitCancelFullScreen) {

               document.webkitCancelFullScreen();

           } else if (document.msExitFullscreen) {

               document.msExitFullscreen();

           }

       },

       /**判断全屏模式是否是可用*/  

       isFullscreenEnabled: function () {

           return document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled || false;

       },

       /**判断整个页面被一个标签铺满*/  

       isFullscreenForNoScroll: function () {

           var explorer = window.navigator.userAgent.toLowerCase();

           if (explorer.indexOf('chrome') > -1) {/*webkit*/

               return (document.body.scrollHeight === window.screen.height && document.body.scrollWidth === window.screen.width);

           } else {/*IE 9+  fireFox*/

               return (window.outerHeight === window.screen.height && window.outerWidth === window.screen.width);

           }

       },

       /**判断是否全屏*/  

       isFullScreen: function () {

           return document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement ? true : false;

       },

       /**实现局部div、dom元素全屏*/ fullScreenForDOM: function (sel) {

           sel = typeof sel == "string" ? document.querySelector(sel) : sel;

           /**el是具体的dom元素*/var rfs = sel.requestFullScreen || sel.webkitRequestFullScreen || sel.mozRequestFullScreen || sel.msRequestFullScreen, wscript;

           if (typeof rfs != "undefined" && rfs) {

               rfs.call(sel);

               return;

           }

           if (typeof window.ActiveXObject != "undefined") {

               wscript = new ActiveXObject("WScript.Shell");

               if (wscript) {

                   wscript.SendKeys("{F11}");

               }

           }

       },

       /**实现局部div、dom元素退出全屏*/exitFullScreenForDOM: function (sel) {

           sel = typeof sel == "string" ? document.querySelector(sel) : sel;

           /**el是具体的dom元素*/var el = document, cfs = sel.cancelFullScreen || sel.webkitCancelFullScreen || sel.mozCancelFullScreen || sel.exitFullScreen, wscript;

           if (typeof cfs != "undefined" && cfs) {

               cfs.call(el);

               return;

           }

           if (typeof window.ActiveXObject != "undefined") {

               wscript = new ActiveXObject("WScript.Shell");

               if (wscript != null) {

                   wscript.SendKeys("{F11}");

               }

           }

       },

   },

};

相关文章
|
5天前
|
Linux
edac-utils工具如何使用
【6月更文挑战第1天】edac-utils工具如何使用
20 3
|
7月前
|
Java
java模块导出与导入
java模块导出与导入
63 0
【Python】深究模块导入:from .. import ..\ import ..
【Python】深究模块导入:from .. import ..\ import ..
|
Python
python使用import引用不了configparse模块等问题解决办法
python使用import引用不了configparse模块等问题解决办法
142 0
|
算法 Java
Error:java: 不允许在使用 --release 时从系统模块 java.xml 导出程序包:
Error:java: 不允许在使用 --release 时从系统模块 java.xml 导出程序包:
521 0
Error:java: 不允许在使用 --release 时从系统模块 java.xml 导出程序包:
|
Python
Python 实现Web隐藏目录扫描
**Web隐藏目录扫描:** 首先你需要自己寻找一个靠谱的字典,放入脚本根目录并命名为`dict.log`每行一个路径名称.
147 0
|
Python
Python编程:import导入不同目录的模块
Python编程:import导入不同目录的模块
239 0
|
存储 Java Unix
Python中的模块、包、import module1,mudule2、from…import、from … import *、as、定位模块、模块中的__all__和__name__、模块的注意点
在Python中用关键字import来引入某个模块,比如要引用模块math,就可以在文件最开始的地方用import math来引入想一想:为什么必须加上模块名调用呢?因为可能存在这样一种情况:在多个模块中含有相同名称的函数,此时如果只是通过函数名来调用,解释器无法知道到底要调用哪个函数。所以如果像上述这样引入模块的时候,调用函数必须加上模块名通过这种方式引入的时候,调用函数时只能给出函数名,不能给出模块名,但是当两个模块中含有相同名称函数的时候,后面一次引入会覆盖前一次引入。也就是说假如模块A中有函数fun
159 1
Python中的模块、包、import module1,mudule2、from…import、from … import *、as、定位模块、模块中的__all__和__name__、模块的注意点
Mac Jmeter导入外部.jmx文件时报错“com.thoughtworks.xstream.converters.ConversionException”
Mac Jmeter导入外部.jmx文件时报错“com.thoughtworks.xstream.converters.ConversionException”
Mac Jmeter导入外部.jmx文件时报错“com.thoughtworks.xstream.converters.ConversionException”
|
Java
IDEA:修改JAVA文件自动引入import.*包
IDEA:修改JAVA文件自动引入import.*包
651 0
IDEA:修改JAVA文件自动引入import.*包

热门文章

最新文章