Tell me how to solve this issue or make an analog. On the page, the data is entered and the aliases are saved with their recording in the database. Required, if the person did not save, but simply decided to exit, close, refresh, or switch to another page, the script ( ajax ) should be executed, which clears everything in the database and other places (i.e., cancels the user's actions). Cleaning occurs after the exit window confirms leaving or page refresh. When you click on save - everything should be saved without any questions. I used the following code:

 window.addEventListener("beforeunload", function (e) { var confirmationMessage = "1"; (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //Webkit, Safari, Chrome }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Everything works out as it should, with one very serious exception! When you click on the "Save" button, this code also works and as a result everything is deleted. Button code (all of a sudden it's important):

 <div class="btn-group"> <button type="button" id="jsubmitbtn" class="btn btn-primary itemsave" onclick="Joomla.submitbutton('article.save')"> <span class="icon-ok"></span>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ </button> </div> 
Apparently due to the fact that after saving a redirect occurs (and nothing can be done with it, it is needed), then beforeunload works. Maybe maybe somehow filter this moment?

Addition

as far as I understood initially, .unload() is called only if the "answer" is positive in the beforeunload dialog box. This is probably my big mistake. Tell me how to properly connect it to my design?

 var userClickedSave = false; jsubmitbtn.addEventListener('click', (e)=> { e.preventDefault(); userClickedSave = true; Joomla.submitbutton('article.save'); }); window.addEventListener("beforeunload", function (e) { if (userClickedSave) return; var confirmationMessage = "1"; (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //Webkit, Safari, Chrome }); // удаляСм строку ΠΈΠ· Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ рСзСрвирования алиаса ΠΏΡ€ΠΈ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΈ ΠΈΠ»ΠΈ ΠΏΠΎΠΊΠΈΠ΄Π°Π½ΠΈΠΈ страницы $(window).unload(function () { $.ajax({ type: "POST", url: '../Π΅Π΅Π΅/file.php', async : false, data: { 'aaa': aaa, 'bbb': bbb }, success: function(response) { // console.log(response); }, error: function() { console.log('ошибка!'); } }); }); } 
 <button type="button" id="jsubmitbtn" class="btn btn-primary itemsave" onclick="userClickedSave = true;Joomla.submitbutton('article.save')"> <span class="icon-ok"></span><?php echo JText::_('JSAVE') ?> </button> 

Solution (in my case)

and both helped a lot, thank you very much! I will mark it as an "answer" (I just don’t know if both are possible, probably not). The question was not completely solved by your answers, but apparently I did not correctly ask it.

 var userClickedSave = false; jsubmitbtn.addEventListener('click', (e)=> { e.preventDefault(); userClickedSave = true; Joomla.submitbutton('article.save'); }); function pageCleanup() { if (!userClickedSave) { // удаляСм строку ΠΈΠ· Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ рСзСрвирования алиаса ΠΏΡ€ΠΈ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΈ ΠΈΠ»ΠΈ ΠΏΠΎΠΊΠΈΠ΄Π°Π½ΠΈΠΈ страницы unloadDelAjax(); // фнкция с ajax запросом } } $(window).on('beforeunload', function (e) { if (userClickedSave) return; var confirmationMessage = "1"; (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //this will work only for Chrome pageCleanup(); }); $(window).on("unload", function () { //this will work for other browsers pageCleanup(); }); 

    2 answers 2

     <script> var userClickedSave = false; </script> <div class="btn-group"> <button type="button" id="jsubmitbtn" class="btn btn-primary itemsave" onclick="userClickedSave = true;Joomla.submitbutton('article.save')"> <span class="icon-ok"></span>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ </button> </div> window.addEventListener("beforeunload", function (e) { if (userClickedSave) return; var confirmationMessage = "1"; (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //Webkit, Safari, Chrome }); 
    • logically, the written looks right, but in fact the dialogue from beforeunload still comes out and therefore, when confirmed, $(window).unload(function () { ... } - medvedev works
     jsubmitbtn.addEventListener('click', (e)=> { e.preventDefault(); userClickedSave = true; Joomla.submitbutton('article.save'); }); 

    Calling preventDefault will prevent standard browser behavior (redirect)

    • If you understand correctly, you should also add window.addEventListener("beforeunload", function (e) { if (userClickedSave) return; in this case there is no dialog box, but $(window).unload(function () { ... } also works - medvedev