更新时间:2019-12-11 亲测可用
一、禁止浏览器回退/返回
// 禁止浏览器回退/返回
function banReturn() {
XBack = {};
(function (XBack) {
XBack.STATE = 'x - back';
XBack.element;
XBack.onPopState = function (event) {
event.state === XBack.STATE && XBack.fire();
XBack.record(XBack.STATE); //初始化事件时,push一下
};
XBack.record = function (state) {
history.pushState(state, null, location.href);
};
XBack.fire = function () {
var event = document.createEvent('Events');
event.initEvent(XBack.STATE, false, false);
XBack.element.dispatchEvent(event);
};
XBack.listen = function (listener) {
XBack.element.addEventListener(XBack.STATE, listener, false);
};
XBack.init = function () {
XBack.element = document.createElement('span');
window.addEventListener('popstate', XBack.onPopState);
XBack.record(XBack.STATE);
};
})(XBack);
XBack.init();
XBack.listen(function () { });
}
二、监听浏览器返回/离开
// 监听浏览器返回离开事件, 必须先有history.pushState() 才能使用这个这 popstate 方法监听
window.addEventListener("popstate", function(e) {
console.log("我监听到页面离开了")
// window.location.href = 'XXX'; // 可以指定返回哪一个页面。或者做其他业务相关的逻辑操作
}, false);
// 该方法添加了一个历史记录,路径为#,保证了不刷新页面,不直接后退。
pushHistory();
function pushHistory() {
var state = {
title: "title", // 这可以随意填写
url: "#"
};
window.history.pushState(state, "title", "#");
}
三、关闭微信浏览器页面
//关闭微信浏览器页面
function weixinClosePage() {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage);
document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage);
}
} else {
WeixinJSBridge.call('closeWindow');
}
}
四、修改默认的alert弹框
// 修改默认的alert弹框
window.alert = function (name) {
var iframe = document.createElement("IFRAME");
iframe.style.display = "none";
iframe.setAttribute("src", 'data:text/plain,');
document.documentElement.appendChild(iframe);
window.frames[0].window.alert(name);
iframe.parentNode.removeChild(iframe);
};
五、获取地址栏的参数
// 获取地址栏的参数
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
六、cookie增删改查
// 获取cookie
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// 设置cookie 或修改cookie
function setCookie(objName, objValue, objDays) {
var str = objName + "=" + escape(objValue);
if(objDays > 0) {
// objDays 单位天
var date = new Date();
var ms = objDays * 24 * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
// 永久保存
if(objDays === Infinity){
str += "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
}
str += "; path=/";
document.cookie = str;
};
// 删除cookie
function delCookie(objName) {
// 设置为过去的日期即可删除
document.cookie = objName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}