Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

javascript - 如何使用JavaScript停止浏览器后退按钮(how to stop browser back button using javascript)

I am doing an online quiz app in php.(我正在用php做一个在线测验应用程序。)

I want to restrict the user from going back in an exam.(我想限制用户再次参加考试。) I have tried the following script but it stops my timer.(我尝试了以下脚本,但它停止了我的计时器。) What should I do?(我该怎么办?) I have included the source code.(我已经包含了源代码。) The timer is stored in cdtimer.js(计时器存储在cdtimer.js中) <script type="text/javascript"> window.history.forward(); function noBack() { window.history.forward(); } </script> <body onLoad="noBack();" onpageshow="if (event.persisted) noBack();" onUnload=""> I have the exam timer which takes a duration for the exam from a mysql value.(我有考试计时器,该计时器从mysql值中获取考试时间。) The timer starts accordingly but it stops when I put the code in for disabling the back button.(计时器相应地启动,但是当我放入用于禁用后退按钮的代码时,计时器停止。) What is my problem?(我怎么了)   ask by Sawant Akshay translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are numerous reasons why disabling the back button will not really work.(有许多原因导致禁用后退按钮无法真正起作用。)

Your best bet is to warn the user:(最好的选择是警告用户:) window.onbeforeunload = function() { return "Your work will be lost."; }; This page does list a number of ways you could try to disable the back button, but none are guaranteed:(该页面的确列出了您可以尝试禁用“后退”按钮的多种方法,但不能保证任何一种方法:) http://www.irt.org/script/311.htm(http://www.irt.org/script/311.htm)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...