📜 ⬆️ ⬇️

Added jQuery FormNavigate or “don't let the user lose data”

Once on the open spaces, Habra met the FormNavigate plugin (requiring the user to confirm the closing of the tab or follow the link when the form is filled in, a la gmail), and once it was required to even apply it.

But the kind in which the plugin did not suit me and I allowed myself to rewrite it a bit.

So, for example, it was inconvenient for me to choose those links on which confirmations should be caught, but, on the contrary, it was required to indicate references to which the action of the plugin will not apply. Here advanced developers will start throwing me with tomatoes, that I don’t know the correct work of selectors in jQuery. But this is not the case, you can check how the previous version of the plugin works, for example, for: $('a:not([class~="ajax"])') (suggested by the author of the last topic $('a:[class!="ajax"]') generally do miracles).

What a shame to hide, I allowed myself to rewrite the plugin a bit:
  1. jQuery.fn.extend ({
  2. FormNavigate: function (o) {
  3. var formdata_original = true ;
  4. jQuery (window) .bind ( 'beforeunload' , function () {
  5. if (! formdata_original) return settings.message;
  6. });
  7. var def = {
  8. message: '' ,
  9. aOutConfirm: 'a: not ([target! = _ blank])'
  10. };
  11. var settings = jQuery.extend ( false , def, o);
  12. if (o.aOutConfirm && o.aOutConfirm! = def.aOutConfirm) {
  13. jQuery ( 'a' ) .addClass ( 'aOutConfirmPlugin' );
  14. jQuery (settings.aOutConfirm) .removeClass ( "aOutConfirmPlugin" );
  15. jQuery (settings.aOutConfirm) .click ( function () {
  16. formdata_original = true ;
  17. return true ;
  18. });
  19. }
  20. jQuery ( "a.aOutConfirmPlugin" ) .click ( function () {
  21. if (formdata_original == false )
  22. if (confirm (settings.message))
  23. formdata_original = true ;
  24. Return formdata_original;
  25. });
  26. jQuery ( this ) .find ( "input [type = text], textarea, input [type = 'password']], input [type = 'radio'], input [type = 'checkbox'], input [type = 'file '] " ) .live ( ' change keypress' , function ( event ) {
  27. formdata_original = false ;
  28. });
  29. jQuery ( this ) .find ( ": submit, input [type = 'image']" ) .click ( function () {
  30. formdata_original = true ;
  31. });
  32. }
  33. });
* This source code was highlighted with Source Code Highlighter .

')
Example of use:
  1. $ ( document ) .ready ( function () {
  2. $ ( "#changeme" ) .FormNavigate ({
  3. message: "Content has been changed! \ nAre you sure you want to leave the page without saving?" ,
  4. aOutConfirm: "a.ignore"
  5. });
  6. });
* This source code was highlighted with Source Code Highlighter .

Where
message - text information in the confirmation window,
aOutConfirm - tags ignored for confirmation.

Fixed some problems, for example, overriding the onbeforeunload event, incorrect work with the text field (textarea).

Download the corrected version .
See an example .

I hope you find this useful!

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


All Articles