1

My question is it possible to display a message if browser buttons have been pressed in order to be able to display a message stating if user should leave the page or stay on a page? Also can a message be displayed if user tries to change a url if they are on a certain page?

Thanks

user1941871
  • 139
  • 2
  • 9
  • 7
    Serious flaw in your approach... user can easily disable javascript. You need to control what they can or can't see from server. Also different browsers have different security policies regarding what you can display as a message in `onbeforeunload` – charlietfl Jan 06 '13 at 23:21
  • https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/beforeunload – SLaks Jan 06 '13 at 23:22
  • @charlietfl Yes but I need to create an exam system on a browser. Is there a better approach to this because if there is no approach then user can navigate to other pages and leave their creation of exams unfinished – user1941871 Jan 06 '13 at 23:25
  • yes... you need to manage it from server – charlietfl Jan 06 '13 at 23:25
  • @charlietfl Ok I will definitly need a sample code to see how this could be done then because that I wil be struggling to code it. If I look at your code hen I can manipulate it to fit my code and hoefully this problem can be sorted – user1941871 Jan 06 '13 at 23:27
  • wait you're getting paid to do this, right? Then you should code it! I know how to code your whole application actually, but there's way easier questions to answer on here for just some upvotes and SO points. – sajawikio Jan 06 '13 at 23:32
  • suggest you read up on php sessions and browser history. – charlietfl Jan 06 '13 at 23:32
  • @sajawikio Im not getting paid to do this, Im just a postgraduate student but my strength is SQL/Oracle, Im not really much of a web developer but Im stuck with trying to deal with this problem. – user1881090 Jan 06 '13 at 23:34
  • see this question and answers, this should help you: http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own – hereandnow78 Jan 06 '13 at 23:35
  • @hereandnow78 that post is too old , many browsers have changed policies since then – charlietfl Jan 06 '13 at 23:37
  • Look at this question http://stackoverflow.com/questions/4615813/preventing-duplicate-form-submissions. Using the browser's back and forward button is common problem. – Olaf Dietsche Jan 07 '13 at 00:08

2 Answers2

1
  1. Server side is needed for foolproofing this. On the server, when assessment is started, you should first have start screen URL. You must program it so that once assessment is started (after clicking start), it goes to the exam URL, and your assessment is always saved - when user goes back, using server side tricks have it so that instead of showing the start screen, it redirects back to the correct page (in a way that still maintains history), shows the exam with the data still in it, and pops up the warning not to click back button. Then, if user does it again, it does the same thing, again.

  2. You have to have it so the exam is saved all the time. unload javascript event can be used to notify via alert() that they shouldn't be leaving the exam, and thats about all. onbeforeunload event can be used to try to give them the choice to stay, but shouldn't be depended on as it doesn't work on every browser.

sajawikio
  • 1,516
  • 7
  • 12
  • As I mentioned to sajawikio, my problem is coding the issue, I do nee help coding it so if you can show a sample, then I can manipulate it into my application and get it working. I upvoted your answer for the description but I really need to see what the code is for me to be able to do it. – user1941871 Jan 06 '13 at 23:31
  • maybe for pay but for an SO answer this high level answer is about all I will give. – sajawikio Jan 06 '13 at 23:34
0

Use onbeforeunload as suggested in a comment. There are various example, like this one: https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

If you want to call out the buttons that the user must click, you will need to do some browser detection, as most browsers have OK/Cancel buttons on the window that they display, but Chrome has "Leave this page" and "Stay on this page".

Mihai Danila
  • 2,229
  • 1
  • 23
  • 28
  • Hi, I have tested the page on all the browsers but it does not work on opera – user1941871 Jan 07 '13 at 01:54
  • Apparently not supported in Opera. See http://stackoverflow.com/questions/390260/onbeforeunload-in-opera (a bit old), and this: http://my.opera.com/community/forums/topic.dml?id=880402 – Mihai Danila Jan 07 '13 at 20:41