I am doing online quiz app in php. I want to restrict user while going back in exam. I have tried following script but it stops my timer. What should I do please suggest me. I have included source code. The timer is stored in 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 exam timer which takes duration of exam from mysql db and starts timer accordingly it stops when I put code for disabling back button. what will be the problem.
I create one HTML page ( index.html ). I also create a one ( mechanism.js ) inside a ( script ) folder / directory. Then, I lay all my content inside of ( index.html ) using form, table, span, and div tags as needed. Now, here's the trick that will make back / forward do nothing!
First, the fact that you have only one page! Second, the use of JavaScript with span / div tags to hide and display content on the same page when needed via regular links!
Inside ' index.html ' :
Inside ' mechanism.js ' :
It looks hairy, but note the function names and calls, embedded HTML, and the span tag id calls. This was to show how you can inject different HTML into same span tag on same page! How can Back/Forward affect this design? It cannot, because you are hiding objects and replacing others all on the same page!
How to hide and display? Here goes: Inside functions in ' mechanism.js ' as needed, use:
Inside ' index.html ' call functions through links:
and
I hope I did not give you a headache. Sorry if I did :-)
Try it with ease :
I came across this, needing a solution which worked correctly and "nicely" on a variety of browsers, including Mobile Safari (iOS9 at time of posting). None of the solutions were quite right. I offer the following (tested on IE11, FireFox, Chrome & Safari):
Note the following:
history.forward()
(my old solution) does not work on Mobile Safari --- it seems to do nothing (i.e. the user can still go back).history.pushState()
does work on all of them.history.pushState()
is a url. Solutions which pass a string like'no-back-button'
or'pagename'
seem to work OK, until you then try a Refresh/Reload on the page, at which point a "Page not found" error is generated when the browser tries to locate a page with that as its Url. (The browser is also likely to include that string in the address bar when on the page, which is ugly.)location.href
should be used for the Url.history.pushState()
is a title. Looking around the web most places say it is "not used", and all the solutions here passnull
for that. However, in Mobile Safari at least, that puts the page's Url into the history dropdown the user can access. But when it adds an entry for a page visit normally, it puts in its title, which is preferable. So passingdocument.title
for that results in the same behaviour.