Previous adventures

Problem

Using $(window).bind("beforeunload", ...[snip] to ask if the user wants to leave the page, some strange results appear in some browsers. A dialog will appear and ask if you want to stay on the page or leave the page If you click the "Stay on the page" button, Google Chrome (the latest version is 13.0.782.215 m) and WinIE7 will display the last page of the history on the "Back" button.

To illustrate this point, the current session history consists of visiting the following pages:

  • Page 3
  • Page 2
  • Page 1

You decided to get out of the fire beforeunload events. You have decided to stay on the current page.

You press the back button again and this time decide to leave the page. You should find yourself on page 2, but instead you are on page 1. However, you can again move forward to the page.

Question

How can I prevent the browser from deleting these pages from the back button when we select the option to "stay on the current page"? Is it possible?

Problem example

Using Google Chrome in a new window, go to /qaru.site / ... and look through a few questions without returning. Then click the Ask Question button. Start typing a dialog box. Click Back in your browser. Choose a stay on the page. Click again in the browser and this time select the "Leave Page" option. Now you are back two pages in history. You can essentially return to multiple pages if you click "stay on page" more than once.

Why does it matter ?!

I do not want to confuse my users. This is not a computer based approach.

My code (not so that it is relevant)

 $(window).bind("beforeunload", function(){ if (in_edit > 0){ return "You are currently editing this job.\n\nAre you sure you wish to exit the page?"; } }); 
+6
source share
2 answers

Chrome solved the problem in 2012: You can view the information here: https://code.google.com/p/chromium/issues/detail?id=93858

+1
source

You can try to include a hidden iframe on the page, and then in the onbeforeunload function change src on that iframe. This could probably include a different URL in the story, which could be removed from the story stack instead of the current page you're on.

So the story looks like this:

 Page3 Page2 Page1 

onbeforeunload modifies the iframe:

 Dummy Page3 Page2 Page1 

But this is just an assumption that this might work, you need to try this ...

0
source

Source: https://habr.com/ru/post/895649/


All Articles