·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

js禁止页面刷新与后退的方法

219 1
发表于 2022-3-4 15:07:33 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本文实例讲述了js禁止页面刷新与后退的方法。分享给大家供大家参考。具体实现方法如下:

[JavaScript]  
  1. <script language="javascript">
  2. //禁止用F5键
  3. function document.onkeydown()
  4. {   
  5. if ( event.keyCode==116)
  6.   {   
  7.     event.keyCode = 0;
  8.     event.cancelBubble = true;
  9.     return false;
  10.   }
  11. }
  12. //禁止右键弹出菜单
  13. function document.oncontextmenu()
  14. {   
  15.    return false;   
  16. }
  17. //下面代码实现全屏显示
  18. function window.onload(){
  19.    var Request = new Array();//保存参数
  20.    var s = location.search.substring(1);
  21.    if (s && s!=""){
  22.     var list = s.split("&");
  23.     for (var i=0; i < list.length; i++){
  24.     var pair = list[i].split("=");
  25.     if (pair[0] && pair[0] !=""){
  26.     Request[unescape(pair[0])] = unescape(pair[1]);
  27.     }
  28.     }
  29.    }
  30.    var fullscreen=Request["fullscreen"];
  31.    if(fullscreen!="yes"){
  32.     var file =self.location;
  33.     var a = window.open("about:blank","","fullscreen=yes");
  34.     self.opener=null;
  35.     self.close();
  36.     a.location=file + "?fullscreen=yes";
  37.    }
  38. }
  39. </script>
  40. <script language="Javascript"><!--
  41. //屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键  
  42. //Author: meizz(梅花雨) 2002-6-18  
  43. function document.oncontextmenu(){event.returnValue=false;}
  44. //屏蔽鼠标右键
  45. function window.onhelp(){return false}
  46. //屏蔽F1帮助  
  47. function document.onkeydown()  
  48. {  
  49. if ((window.event.altKey)&&  
  50. ((window.event.keyCode==37)|| //屏蔽 Alt+ 方向键 ←  
  51. (window.event.keyCode==39))) //屏蔽 Alt+ 方向键 →  
  52. {  
  53. alert("不准你使用ALT+方向键前进或后退网页!");  
  54. event.returnValue=false;  
  55. }  
  56. /* 注:这还不是真正地屏蔽 Alt+ 方向键,
  57. 因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放,
  58. 用鼠标点掉警告框,这种屏蔽方法就失效了。以后若
  59. 有哪位高手有真正屏蔽 Alt 键的方法,请告知。*/
  60. if ((event.keyCode==8) || //屏蔽退格删除键  
  61. (event.keyCode==116)|| //屏蔽 F5 刷新键  
  62. (event.ctrlKey && event.keyCode==82)){ //Ctrl + R  
  63. event.keyCode=0;  
  64. event.returnValue=false;  
  65. }  
  66. if (event.keyCode==122){event.keyCode=0;event.returnValue=false;}
  67. //屏蔽F11  
  68. if (event.ctrlKey && event.keyCode==78) event.returnValue=false;
  69. //屏蔽 Ctrl+n  
  70. if (event.shiftKey && event.keyCode==121)event.returnValue=false;
  71. //屏蔽 shift+F10  
  72. if (window.event.srcElement.tagName == "A" && window.event.shiftKey)  
  73. window.event.returnValue = false;
  74. //屏蔽 shift 加鼠标左键新开一网页  
  75. if ((window.event.altKey)&&(window.event.keyCode==115))
  76. //屏蔽Alt+F4  
  77. {  
  78. window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");  
  79. return false;  
  80. }  
  81. }  
  82. </script>
复制代码
我要说一句 收起回复

评论1

AI智能体Lv.7 发表于 2025-3-21 08:28:50 | 查看全部
在Discuz!开发中,如果你需要实现禁止页面刷新与后退的功能,可以通过JavaScript来实现。以下是一些常见的实现方法,结合你提供的代码片段,我将对其进行详细解释和优化。

### 1. 禁止F5刷新页面
你可以通过监听键盘事件来禁止用户按下F5键刷新页面。以下是优化后的代码:

  1. document.onkeydown = function(event) {
  2.     event = event || window.event;
  3.     if (event.keyCode == 116) { // F5键的keyCode是116
  4.         event.preventDefault(); // 阻止默认行为
  5.         event.returnValue = false; // 兼容IE
  6.         return false;
  7.     }
  8. };
复制代码


### 2. 禁止右键菜单
通过监听`contextmenu`事件,可以禁止右键菜单的弹出:

  1. document.oncontextmenu = function() {
  2.     return false; // 禁止右键菜单
  3. };
复制代码


### 3. 全屏显示
你提供的代码片段中有一个全屏显示的功能,但代码有一些问题。以下是修正后的代码:

  1. window.onload = function() {
  2.     var Request = {}; // 使用对象来保存参数
  3.     var s = location.search.substring(1);
  4.     if (s && s != "") {
  5.         var list = s.split("&");
  6.         for (var i = 0; i < list.length; i++) {
  7.             var pair = list[i].split("=");
  8.             if (pair && pair != "") {
  9.                 Request[pair[0]] = unescape(pair[1]);
  10.             }
  11.         }
  12.     }
  13.     var fullscreen = Request['fullscreen'];
  14.     if (fullscreen != "yes") {
  15.         var file = self.location.href;
  16.         var a = window.open("about:blank", "", "fullscreen=yes");
  17.         self.opener = null;
  18.         self.close();
  19.         a.location = file + "?fullscreen=yes";
  20.     }
  21. };
复制代码


### 4. 禁止后退按钮
禁止后退按钮的实现比较复杂,通常可以通过以下几种方式来实现:

- **使用`history.pushState`方法**:通过修改浏览器的历史记录来阻止用户后退。
- **禁用浏览器的后退按钮**:这通常需要结合服务器端逻辑来实现,比如在用户点击后退按钮时重定向到当前页面。

以下是一个使用`history.pushState`的示例:

  1. window.onload = function() {
  2.     history.pushState(null, document.title, location.href);
  3.     window.addEventListener('popstate', function(event) {
  4.         history.pushState(null, document.title, location.href);
  5.     });
  6. };
复制代码


### 5. 综合实现
将以上功能综合起来,可以实现一个较为完整的禁止页面刷新与后退的功能:

  1. document.onkeydown = function(event) {
  2.     event = event || window.event;
  3.     if (event.keyCode == 116) { // F5键
  4.         event.preventDefault();
  5.         event.returnValue = false;
  6.         return false;
  7.     }
  8. };

  9. document.oncontextmenu = function() {
  10.     return false; // 禁止右键菜单
  11. };

  12. window.onload = function() {
  13.     history.pushState(null, document.title, location.href);
  14.     window.addEventListener('popstate', function(event) {
  15.         history.pushState(null, document.title, location.href);
  16.     });

  17.     var Request = {};
  18.     var s = location.search.substring(1);
  19.     if (s && s != "") {
  20.         var list = s.split("&");
  21.         for (var i = 0; i < list.length; i++) {
  22.             var pair = list[i].split("=");
  23.             if (pair && pair != "") {
  24.                 Request[pair[0]] = unescape(pair[1]);
  25.             }
  26.         }
  27.     }
  28.     var fullscreen = Request['fullscreen'];
  29.     if (fullscreen != "yes") {
  30.         var file = self.location.href;
  31.         var a = window.open("about:blank", "", "fullscreen=yes");
  32.         self.opener = null;
  33.         self.close();
  34.         a.location = file + "?fullscreen=yes";
  35.     }
  36. };
复制代码


### 注意事项
- **用户体验**:禁止页面刷新和后退可能会影响用户体验,建议在特定场景下使用,比如考试系统、支付页面等。
- **浏览器兼容性**:不同浏览器对JavaScript的支持可能有所不同,建议在实际使用前进行充分测试。

希望这些方法能帮助你实现所需的功能。如果你有更多关于Discuz!开发的问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:3.137.136.226,GMT+8, 2025-4-21 02:33 , Processed in 1.254736 second(s), 76 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表