Javascript 我可以在我的 web 应用程序中禁用浏览器刷新吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5711815/
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
Can I disable browser refresh in my webapp?
提问by kiran
I want to disable refresh on my website. Is there any way to disable F5, Ctrl-R, or reload in a webpage?
我想在我的网站上禁用刷新。有没有办法在网页中禁用F5、Ctrl-R或重新加载?
采纳答案by Waqas Raja
I have a suggestion. There is an answer box in this page.
我有一个建议。此页面中有一个答案框。
(Here below with Your Answer label)
(此处带有您的答案标签)
- Type some text in it.
- Then refresh this page
- 在其中键入一些文本。
- 然后刷新这个页面
When you refresh there is some warning message displayed, I suggest you to display the similar warning message, instead of disabling the refresh functionality.
当您刷新时会显示一些警告消息,我建议您显示类似的警告消息,而不是禁用刷新功能。
回答by Wesley Murch
window.onbeforeunload = function () {return false;}
This disables leaving the page unless the user confirms, not onlyrefresh, so you'll have to code this yourself to get it to work.
这将禁止离开页面,除非用户确认,不仅刷新,因此您必须自己编写代码才能使其正常工作。
You never said what you were doing with it so I left it at that.
你从来没有说过你用它做什么,所以我把它留在了那里。
回答by alex
No, there is no way to disable it.
不,没有办法禁用它。
If you are trying to avoid the "Do you want to submit this action again?"dialog, use Post/Redirect/Get.
如果您试图避免“您想再次提交此操作吗?” 对话框,使用Post/Redirect/Get。
回答by Gideon
Check for a cookie every time you enter the page. if the cookie is there then you know a refresh was made.
每次进入页面时检查cookie。如果 cookie 存在,那么您就知道进行了刷新。
If the cookie is not there create one with zero duration (exist only till the browser still open)
如果 cookie 不存在,则创建一个持续时间为零的 cookie(仅在浏览器打开时才存在)
回答by SpYk3HH
I don't know how to disable refresh clicking (i still need that one myself) Other than using a server side script to stop the "redirect" HOWEVER, I do know of EXTREMELY easy jQuery to disable F5:
我不知道如何禁用刷新点击(我自己仍然需要那个)除了使用服务器端脚本来停止“重定向”但是,我知道非常简单的 jQuery 禁用F5:
function disableF5(e) { if (e.which == 116) e.preventDefault(); };
// To disable f5
$(document).bind("keydown", disableF5);
// To re-enable f5
$(document).unbind("keydown", disableF5);
回答by Jigar Joshi
No,There is no such way. You can just warn user for not to refresh
不,没有这样的方法。您可以警告用户不要刷新
回答by Merlyn Morgan-Graham
There may be some tricks that would work on some browsers, but there is nothing that could guarantee that they couldn't refresh the page. Browsers and web standards are wide open, so a user could easily hack up a browser to work around anything you implemented.
可能有一些技巧可以在某些浏览器上工作,但是没有什么可以保证它们无法刷新页面。浏览器和网络标准是开放的,因此用户可以轻松破解浏览器来解决您实施的任何问题。
If refreshing would negatively impact the user experience, it is best to give them a textual warning.
如果刷新会对用户体验产生负面影响,最好给他们一个文字警告。
If this is to fix a bug (such as a lack of idempotency), you should look into fixing the bug instead.
如果这是为了修复错误(例如缺乏幂等性),您应该考虑修复错误。
回答by Ejardy
There is a way to prevent the user from refreshing your page by pressing F5. Try this codes inside your body tag:
有一种方法可以通过按 F5 来防止用户刷新您的页面。在你的身体标签中试试这个代码:
<body onkeydown="return (event.keyCode != 116)">
回答by Simran Jeet Singh Bagga
If you want to disable ctrl+f5 , ctrl+R , f5 ,backspace then you can use this simple code. This code is working in Mozila as well as Chrome . Add this code inside your body tag:
如果你想禁用 ctrl+f5 , ctrl+R , f5 , backspace 那么你可以使用这个简单的代码。此代码适用于 Mozila 和 Chrome 。将此代码添加到您的 body 标签中:
<body onkeydown="return (event.keyCode == 154)">