3

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.

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.

jatin_ghataliya
  • 144
  • 1
  • 1
  • 16
Amogh
  • 4,453
  • 11
  • 45
  • 106
  • You can visit this. http://stackoverflow.com/questions/8078650/can-we-disable-browsers-buttonsback-forward-refresh-by-javascript – Bindiya Patoliya Jul 31 '13 at 05:26
  • @BindiyaPatoliya any alternative... – Amogh Jul 31 '13 at 05:28
  • No,i don't have any alternative.Sorry :) – Bindiya Patoliya Jul 31 '13 at 05:33
  • 1
    How are you going to prevent the user from using a developer toolbar/debugger to circumvent your scripts, or maybe to disable javascript at all? – Michaël Hompus Jul 31 '13 at 05:55
  • 1
    This won't work. For example any savvy student could throw together a bookmarklet that kills the timer by issuing a client-side JS command to cancel the timeout/interval. Pass it out to their friends and no one needs to worry about time limits anymore. You need to implement actual server-side code for this or accept that it's easily attacked and badly coded. – Chris Moschini Jul 31 '13 at 05:55
  • @ChrisMoschini can I user – Amogh Jul 31 '13 at 06:11
  • @Amogh No. As long as the thing you're trying to enforce on the user is done in Javascript and not on the server, the user can thwart/trick/manipulate whatever they want. – Chris Moschini Jul 31 '13 at 06:16
  • @ChrisMoschini Thanks for your valuable guidelines...on internet there are son many frameworks for online tests they also have timer.. how they implemented that timer is that in JavaScript or any think else... – Amogh Jul 31 '13 at 06:22
  • Either they enforce that same timer on the server side, or they're easily attacked and rendered useless. – Chris Moschini Jul 31 '13 at 07:49
  • @ChrisMoschini as this project is developed in Spring MVC...so we are trying to implement java applet for the same. – Amogh Jul 31 '13 at 09:28

8 Answers8

10

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");
Rishabh Mishra
  • 498
  • 8
  • 21
Developer
  • 6,292
  • 19
  • 55
  • 115
4

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";
};
Elayaraja Dev
  • 208
  • 3
  • 6
1
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});
  • this will prevent browsers from back – Nilesh chauhan Feb 06 '17 at 14:24
  • Welcome to stack overflow :-) Please look at [answer]. You should provide some information why your code solves the problem. Code-only answers aren't useful for the community. – JimHawkins Feb 06 '17 at 15:41
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Baum mit Augen Feb 06 '17 at 22:11
1

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.

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";}
Vishal P Gothi
  • 987
  • 5
  • 15
0

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

Engineer
  • 5,911
  • 4
  • 31
  • 58
  • @sankalp Mishra if we i use the way you said,then in mozilla it asks for the permission to open for a new pop window.If permitted then only its opening –  Aug 01 '13 at 05:27
0

Try this:

function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); };
coder
  • 13,002
  • 31
  • 112
  • 214
0
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();
}     
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
-1

on document.ready I added this line..

window.history.forward(-1)

Amogh
  • 4,453
  • 11
  • 45
  • 106