javascript 如何仅从我的登录页面禁用我的 mvc 中的浏览器后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27403062/
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
How to disable the browser back button in my mvc from only my login page?
提问by Vrankela
I tried this methodby putting it in my login.cshtml but it doesnt work there at all. Then I tried putting it in my _Layout.cshtml but then it does it's job while affecting the entire website (as expected). Any tips on where I should put or how I should manipulate the code?
我通过将它放在我的 login.cshtml 中尝试了这种方法,但它根本不起作用。然后我尝试将它放在我的 _Layout.cshtml 中,但是它在影响整个网站的同时完成了它的工作(正如预期的那样)。关于我应该放在哪里或如何操作代码的任何提示?
here it is here:
在这里:
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
回答by kharr1027
I recently used this in an MVC project. Maybe you can put it on the page that the login redirects to.
我最近在一个 MVC 项目中使用了它。也许你可以把它放在登录重定向到的页面上。
//kill all back button functionality
function noBack() { window.history.forward() }
noBack();
window.onload = noBack;
window.onpageshow = function(evt) { if (evt.persisted) noBack() }
window.onunload = function() { void (0) }
Do be careful though if you are using this for security reasons, as Javascript is not the most ideal solution to handle secure logic within a site. It is easy to get around since the Javascript code is executed on the clients PC and/or it can be disabled by the browser.
如果出于安全原因使用它,请务必小心,因为 Javascript 不是处理站点内安全逻辑的最理想解决方案。由于 Javascript 代码在客户端 PC 上执行和/或可以被浏览器禁用,因此很容易绕过。
回答by Mathieu Labrie Parent
You can put it in the layout but active it only if your on the login page :
您可以将它放在布局中,但仅当您在登录页面上时才激活它:
if(window.location.href.toLowerCase().indexOf("login") > -1)
{
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
}