Hello! I wrote a module under Jumla, which, when closing a tab with a site page, displays a warning message, and a form in which the user indicates the reason for leaving the site. Made according to the principle as here - http://stopexit.ru/

The question is how to make the window closing event work exactly at closing, and not when clicking on the internal links of the site menu ...

this is event code for window on jquery

window.onbeforeunload = function(e){ if($j.cookie('sendForm')==1){ window.onbeforeunload = null; }else{ e = e || window.event; // For IE and Firefox prior to version 4 if(e){ e.returnValue = _onbefore; } openForm(); return _onbefore; // For Safari } }; 

Here is the site on which this module http://g-idea.ru/, when navigating through the items in the top menu, a message flies all the same. I read about this event and when it works:

Actions causing this event

Go to another page directly in the browser or through a link. Close the current window in a browser or bookmark. Reload current page. Manipulating the URL of the loaded page using the location object in JavaScript. The window.navigate method. Method window.open or document.open to open a document in the same window

Ie, in fact, everything works correctly! But how on http://stopexit.ru/demo.html when you go to the internal links window does not crash?

  • Oftop: do you think the user leaving the site will bother writing? Useless idea! IMHO - DemoS
  • 3
    And I would remember the address of the site with this form. =) And I would never go there again. - ling
  • Yes, no one argues, I just need to correctly write the code for this event, on the site example when clicking on internal links from the page stopexit.ru/demo.html the window does not crash , but in my code everything is just like them, but the window crashes (((( - toorr2p

2 answers 2

Solution found!

For all links when loading a document, we assign a function to the click event, in which we reset the event for window onbeforeunload:

 $j(document).ready(function () { $j('a').each(function (index, value) { $j(this).click(function () { window.onbeforeunload = null; }); }); window.onbeforeunload = function (e) {}; }); 
  • one
    And then on all the submissions ... And then on the diva buttons ... - Sh4dow
  • One criticism, even if someone flashed his mind and offered the right option! - toorr2p
  • one
    Perhaps this is all because no one would like to go to such a site) If you have a different, more adequate goal, you would write it and many would like to appear at once) - Sh4dow

Solution from the very "product" page:

 jQuery("a, input").click(function(){window.onbeforeunload=null}); jQuery("form").submit(function(){window.onbeforeunload=null}); jQuery("select").change(function(){window.onbeforeunload=null}); jQuery("select").blur(function(){window.onbeforeunload=null}); jQuery("select").focus(function(){window.onbeforeunload=null}); window.onbeforeunload=function(e){if(getCookie('user_hash_otvet')!=false){window.onbeforeunload=null};...} 

In short, the solution is absolutely not universal and generally moveton:

 jQuery("select").change(...); jQuery("select").blur(...); jQuery("select").focus(...);