$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}");
}
}
},
},
};