出于安全目的,我想通过使用此javascript代码在我的应用程序网站中禁用某些页面,该javascript代码在其他浏览器中也可以正常使用:
history.pushState(null, null, location.href);
window.onpopstate = function () {
history.go(1);
};
但不幸的是,在chrome浏览器中,除非用户在页面上或控制台的开发人员工具中单击(如果您在其中运行任何javascript,例如这样的
console.log('test');
自动激活后退按钮Chrome浏览器。我发现很久以前已经在此页面上讨论过此问题:
Google Chrome Help
答案 0 :(得分:0)
(function (global) {
if(typeof (global) === "undefined") {
throw new Error("window is undefined");
}
var _hash = "!";
var noBackPlease = function () {
global.location.href += "#";
global.setTimeout(function () {
global.location.href += "!";
}, 50);
};
global.onhashchange = function () {
if (global.location.hash !== _hash) {
global.location.hash = _hash;
}
};
global.onload = function () {
noBackPlease();
// disables backspace on page except on input fields and textarea..
document.body.onkeydown = function (e) {
var elm = e.target.nodeName.toLowerCase();
if (e.which === 8 && (elm !== 'input' && elm !== 'textarea')) {
e.preventDefault();
}
// stopping event bubbling up the DOM tree..
e.stopPropagation();
};
}
})(window);
history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function (event)
{
history.pushState(null, document.title, location.href);
});
window.location.hash="no-back-button";
window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history
window.onhashchange=function(){window.location.hash="no-back-button";}
答案 1 :(得分:0)
在那儿,当我遇到很多问题时,我在最新的问题上发布了我对这个问题的解决方案,但也希望它能在这里分享,如果不晚的话。
Heres the link to answer希望对您有所帮助!