使用 jquery 在浏览器中禁用后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3243684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable Back Button in Browser using jquery?
提问by user380979
I would like to Disable Back button when user click on logoff for this i delete only cookies of that user not session in my application. And want to disable Back button
当用户为此单击注销时,我想禁用后退按钮,我只删除该用户的 cookie,而不是我的应用程序中的会话。并想禁用后退按钮
if(getCookie('username') == ""){
Disable back button;// we use "window.history.redirect" for javascript i look jquery for this
}
Please help me..
请帮我..
回答by reza gholami
you must push your url in pushState and clean the browser history:
您必须在 pushState 中推送您的 url 并清理浏览器历史记录:
try this :
尝试这个 :
$(document).ready(function() {
window.history.pushState(null, "", window.location.href);
window.onpopstate = function() {
window.history.pushState(null, "", window.location.href);
};
});
回答by Nabha Narate - Shirkul
Use following code as it is:
按原样使用以下代码:
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
}
else {
ignoreHashChange = false;
}
};
}
};
回答by Sunil Gupta
CODE:
代码:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() {
window.history.forward();
}
</SCRIPT>
</HEAD>
<BODY onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">
</BODY>
</HTML>
回答by Cheezy Code
var path = 'Test.aspx'; //write here name of your page
history.pushState(null, null, path + window.location.search);
window.addEventListener('popstate', function (event) {
history.pushState(null, null, path + window.location.search);
});
This works across all browsers(except < IE10) even if urls have query strings appended.
即使 url 附加了查询字符串,这也适用于所有浏览器(< IE10 除外)。
For this you don't even need jquery.
为此,您甚至不需要 jquery。
have a look hereto apply this for your page.
看看这里将其应用于您的页面。
回答by FK82
This is not exactly your approach, but you may disable the navigation panel of any window using plain javascript. Just set window.menubar
and window.toolbar
visibility to false
as follows,
这不完全是您的方法,但您可以使用纯 JavaScript 禁用任何窗口的导航面板。只需设置window.menubar
和window.toolbar
可见性false
如下,
window.menubar.visible = false ;
window.toolbar.visible = false ;
Update:
更新:
Changing the menubar and toolbar visibility of an existing window seems to violate security protocols. (https://developer.mozilla.org/en/DOM/window.menubar)
更改现有窗口的菜单栏和工具栏可见性似乎违反了安全协议。( https://developer.mozilla.org/en/DOM/window.menubar)
Therefore the only real way is to open a new window with menubar and toolbar set to "no" in the first place:
因此,唯一真正的方法是首先打开一个菜单栏和工具栏设置为“no”的新窗口:
window.open(url,name,"menubar=no,toolbar=no[, additional options]",true) ;
If you set the replace argument (the last argument) to true the new window should also inherit the history of the window it was opened in.
如果您将替换参数(最后一个参数)设置为 true,则新窗口还应继承打开它的窗口的历史记录。
Check https://developer.mozilla.org/en/DOM/window.openand http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspxfor reference.
检查https://developer.mozilla.org/en/DOM/window.open和http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx以供参考。
回答by saidesh kilaru
Simple logic: move forward when click forward
简单逻辑:点击前进时前进
function preventBack() {
window.history.forward();
}
window.onunload = function() {
null;
};
setTimeout("preventBack()", 0);
Keep this js code in your page it works.
将此 js 代码保留在您的页面中,它可以工作。
回答by Ragu
This will work, it worked for me as I am dealing with mobile browsers, android and ios.
这会起作用,它对我有用,因为我正在处理移动浏览器、android 和 ios。
window.history.forward();
window.onload = function()
{
window.history.forward();
};
window.onunload = function() {
null;
};
回答by Ian
Sunil was correct, but to use jQuery paste the code below into any page that you want to prevent going back too. I tested this in IE 11, chrome and FF, which worked fine.
Sunil 是正确的,但要使用 jQuery,请将下面的代码粘贴到您想要防止返回的任何页面中。我在 IE 11、chrome 和 FF 中对此进行了测试,效果很好。
$(document).ready(function () {
function disableBack() {window.history.forward()}
window.onload = disableBack();
window.onpageshow = function (evt) {if (evt.persisted) disableBack()}
});