jQuery 限制用户在任何浏览器中刷新和返回、转发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17962130/
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
Restrict user to refresh and back,forward in any browser
提问by Amogh
I am working on an evaluation system, In which candidate attempt for an exam. So the page where a question and its choices are get rendered is a page with iframe
, In that questions are get the render. The page which holds iframe
contains JavaScript
timer.
我正在研究一个评估系统,其中候选人尝试参加考试。因此,呈现问题及其选择的页面是一个带有 的页面iframe
,在该页面中,问题是获取呈现的。持有的页面iframe
包含JavaScript
计时器。
So when questions get render on the browser to restrict refresh I have written F5
and ctrl+F5
blocking code so candidate can't refresh using keyboard shortcuts but in a browser window, one can still use refresh button or back button. Is there any way to disable this? I know playing with browser functionality is not good practice, So is there any way to get out of this.
因此,当问题在浏览器上呈现以限制刷新时,我编写F5
并ctrl+F5
阻止了代码,因此候选人无法使用键盘快捷键进行刷新,但在浏览器窗口中,仍然可以使用刷新按钮或后退按钮。有什么办法可以禁用它吗?我知道玩浏览器功能不是一个好习惯,所以有什么办法可以摆脱这种情况。
采纳答案by Amogh
on document.ready
I added this line..
在document.ready
我添加了这一行..
window.history.forward(-1)
window.history.forward(-1)
回答by Developer
Try this to disable back button.
试试这个来禁用后退按钮。
<script type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</script>
</head>
<body onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
Or we can remove these from the tool bar
或者我们可以从工具栏中删除这些
window.open ("URL",
"mywindow","status=1,toolbar=0");
回答by Elayaraja Dev
Please try this for restrict back button in all latest browser.
请尝试在所有最新浏览器中限制后退按钮。
history.pushState({ page: 1 }, "Title 1", "#no-back");
window.onhashchange = function (event) {
window.location.hash = "no-back";
};
回答by Nilesh chauhan
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
回答by Vishal P Gothi
It's not possible. Disable caching if you don't want people seeing old pages by going "back", but all they're seeing is things they've just seen anyway so it doesn't make a difference. people having been asking for 20 years how to disable the back button and so far I never seen a site where back button is disables.
这是不可能的。如果您不希望人们通过“返回”来查看旧页面,请禁用缓存,但他们所看到的只是他们刚刚看到的内容,因此没有任何区别。人们已经问了 20 年如何禁用后退按钮,到目前为止,我从未见过禁用后退按钮的网站。
But yes here one trick
但是这里有一个技巧
Thanks
谢谢
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";}
回答by Riyaz Parasara
bool IsPageRefresh = false;
//this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"
if (!IsPostBack)
{
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
Session["SessionId"] = ViewState["ViewStateId"].ToString();
}
else
{
if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
{
IsPageRefresh = true;
}
Session["SessionId"] = System.Guid.NewGuid().ToString();
ViewState["ViewStateId"] = Session["SessionId"].ToString();
}
回答by Sankalp Mishra
You can try this...:-
你可以试试这个...:-
window.open ("http://viralpatel.net/blogs/",
"mywindow","status=1,toolbar=0");
This opens a window without toolbar... NO toolbar no buttons.. :D
这将打开一个没有工具栏的窗口......没有工具栏没有按钮......:D
回答by coder
Try this:
尝试这个:
function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); };