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> 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(); });