简体   繁体   English

如果单击后退浏览器按钮,如何显示警报和确认

[英]How to display alert and confirmation if back browser button has been clicked

I am creating an exam application mainly using php and jquery. 我正在创建一个主要使用php和jquery的考试应用程序。 At the moment the application has these pages in order to for the user to create an exam: 目前,应用程序具有这些页面,以便用户创建考试:

  • Create.php (teacher creates the exam details eg date, time, duration, subject, number of questions, total marks etc) Create.php (教师创建考试详细信息,例如日期,时间,持续时间,主题,问题数量,总分等)
  • QandA.php (teacher creates questions and answers for the exam) QandA.php (老师为考试创建问题和答案)
  • Marks.php (teacher sets up marks for each correct answer(s) per question) Marks.php (老师为每个问题设置每个正确答案的标记)
  • Penalty.php (teacher selects whether their exam will contain penalty marks or not) Penalty.php (老师选择他们的考试是否包含罚分)
  • Finish.php (simply states that your creation of your exam is complete) Finish.php (简单说明您的考试创建完成)

Now the application works fine but there is something I have realized. 现在应用程序工作正常,但有一些我已经意识到。 As this application is run on a browser, the user is able to at anytime click on the Browser back button. 由于此应用程序在浏览器上运行,因此用户可以随时单击“浏览器”后退按钮。 This is going to cause big problems because if I am half way in completing the creation of the exam but click on the browser back button, then I will lose my exam creation progress or worse I am able to do one of the pages again. 这将导致很大的问题,因为如果我在完成考试创建的一半但点击浏览器后退按钮,那么我将失去我的考试创建进度或更糟,我能够再次执行其中一个页面。 This could mean the database which stores the details will contain a half complete exam or an exam which could contain more information than required. 这可能意味着存储详细信息的数据库将包含半完整的考试或考试,其中可能包含超出要求的更多信息。

So this is what I am thinking of doing but I do not know if first of all if it is possible and if it is best practice to do so: 所以这就是我在想的事情,但我不知道如果有可能,以及最好的做法是否:

  • If user clicks on a back browser button while on any of the 4 pages above (except for finish.php) then a confirmation box appears stating "if you go back to previous page then you will lose all your current progress and be sent to menu page (menu.php) 如果用户在上面4个页面中的任何一个页面上点击后面的浏览器按钮(finish.php除外),则会出现一个确认框,指出“如果您返回上一页,那么您将失去当前所有进度并被发送到菜单页面(menu.php)

  • If user clicks on OK in the confirmation box, then it will navigate user to the menu.php page and it will also by AJAX navigate to another php page (deleteentry.php) where it will delete the exam details from the database) 如果用户点击确认框中的确定,那么它将导航用户到menu.php页面,它也将通过AJAX导航到另一个php页面(deleteentry.php),在那里它将从数据库中删除检查详细信息)

  • If user clicks on Cancel then they can continue on current page no problem 如果用户点击取消,那么他们可以继续当前页面没问题

  • If user clicks on back browser button while on finish.php page, then it will display an alert stating "you can't go back, your exax has been sucessfully completed, you will be navigated back to menu page" And all it will do is navigate user to menu.php page and thats it, no ajax to call or anything) 如果用户在finish.php页面上单击后退浏览器按钮,则会显示一条警告,指出“您无法返回,您的exax已成功完成,您将被导航回菜单页面”并且它将会执行所有操作导航用户到menu.php页面就是这样,没有ajax来调用或任何东西)

My question then is that 我的问题是

  1. Is the above ideas a suitable solution to be able to tackle this problem 上述想法是否能够解决这个问题的合适解决方案

  2. How should the code can be written to determine when the back browser button has been pressed depending on the page it was pressed on and be able to perform the commands it is able to perform? 如何编写代码以确定何时按下后退浏览器按钮取决于按下的页面并能够执行它能够执​​行的命令? Also need to know how the AJAX can be written to navigate to deleteentry.php page in background. 还需要知道如何编写AJAX以导航到后台的deleteentry.php页面。

Thank You very much 非常感谢你

I am going to answer the question in your title: 我将在你的标题中回答这个问题:

How to display alert and confirmation if back browser button has been clicked 如果单击后退浏览器按钮,如何显示警报和确认

You can't know if the "back" button was clicked or if the page was refreshed or just page changed -> 您无法知道是否单击了“后退”按钮,或者是否刷新了页面或仅更改了页面 - >

The onunload event will handle all of those cases. onunload事件将处理所有这些情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM