codedance / jquery.AreYouSure

A light-weight jQuery "dirty forms" Plugin - it monitors html forms and alerts users to unsaved changes if they attempt to close the browser or navigate away from the page. (Are you sure?)
508 stars 145 forks source link

Developer friendly suggestion #53

Closed dafriend closed 10 years ago

dafriend commented 10 years ago

While attaching to $.fn there is a check that element is actually a form which silently returns if not working with a form.

  return this.each(function(elem) {
      if (!$(this).is('form')) {
        return;
      }

It might be user friendly to add some kind of notification if the test is true. For example, this use of console before returning.

    return this.each(function(elem) {
      if (!$(this).is('form')) {
        if (console && console.log) {
           console.error("jquery.AreYouSure can only attach to <form> elements");
        }        
        return;
      }

This might help out people who use a selector that's too broad or contains a typo. (Been there...)

Throwing an exception is another possible solution, i.e.

      if (!$(this).is('form')) {
           throw new Error("jquery.AreYouSure can only attach to <form> elements");
      }

A bit more drastic and possibly unwarranted in some circumstances. Highly effective though without adding a lot of weight.

codedance commented 10 years ago

I agree with the suggestion. Anything that makes it easier to avoid hidden behaviours is a good thing. One concern I have is inadvertently adding a regression. I could see the exception method hitting here. The console log may be the best way forward. Thoughts?

I noticed your GitHub account is quite new. Are you interested in giving a pull request a go off the 1.9-dev branch?

dafriend commented 10 years ago

I agree that the console log approach is best. I'll fork 1.9-dev and supply a pull request.

codedance commented 10 years ago

Thanks! Let me know if you need a hand. I plan on rolling 1.9 at the end of this month.

dafriend commented 10 years ago

I issued a pull request. My confidence is low that it was done in the correct way. As you noticed I'm new to GitHub and don't understand all I know. Let me know if I need to follow some other work-flow to make this fit your needs.