Loading ...
Sorry, an error occurred while loading the content.

Interesting Event Bug?

Expand Messages
  • foxxtrot713
    Admittedly, the approach laid out below is naive and probably not best practice, but I encountered what appears to be an interesting bug, and I m unsure if
    Message 1 of 2 , Aug 29, 2008
    • 0 Attachment
      Admittedly, the approach laid out below is naive and probably not best practice, but I encountered what appears to be an interesting bug, and I'm unsure if it's in YUI or the browsers event model (this bug appears in both Firefox and IE).

      I have a page with a form on it, this form has a submit button and everything else a good form that doesn't support JavaScript should have. At the end of the page load, I have JavaScript that sets up the form with an event-based model, and gets rid of the buttons to provide a cleaner interface.  The function that removes the select button looks something like this:

      var PrepareForm = function (form) {
        form.removeChild(form.submit_button);
      }
      

      And this method is set to be run as an event by using the onAvailable method, referring to the form.  But, the application may have multiple forms, so I decided to wrap up each form in a closure, that also wired up all the events for that form, so I did this:
      var PrepareTestForm = function(form) {
        YAHOO.log("Inside PrepareForm");
        var RemoveElements = function () {
          YAHOO.log("Inside OnFormLoad");
          form.removeChild(form.submit_button);
        }
        var OnChangeHandler = function (e) {
          // Do stuff
        }
      
        YAHOO.util.Event.onAvailable(form, RemoveElements);
        YAHOO.tuil.Event.addListener(form.select_box, "change", OnChangeHandler);
      }
      

      But, I don't really want to register an onAvailable for a form that may not be loaded into the page, so rather than setting up an onAvailable call to PrepareTestForm when the "test_form" is available, and since I know the JavaScript is the last thing to run, I opted to put the following loop into the code:
      for(var i = 0 ; i < document.forms.length ; i += 1) {
        switch(document.forms[i].id) {
          case "test_form":
            YAHOO.log("Call PrepareForm");
            PrepareTestForm(document.forms[i]);
            break;
          case "other_form":
            // Set up Form
          case "clown_form":
            // Set up Form.
        }
      }
      

      Now, what's interesting is the output of the log on the above code.  "Call PrepareForm" appears once.  "Inside PrepareForm" appears once.  "Inside OnFormLoad" appears twice?

      If I register calls with onAvailable (both with onAvailable before and after the form's decleration), it works fine.  It's only when I do what I've done above that this occurs, and it's in both FIrefox (2/3) and IE 7.  I've put in some error checking code to make sure that errors aren't thrown when PrepareForm is called twice, but I'm not sure why it's getting called twice at all?  Am I missing something in the Browser Event Model?  Or is YUI doing something that I don't understand?

      Jeff Craig
    • foxxtrot713
      Oh, and since I forgot to include it, I ve put a sample page up here at: http://catalog.wsu.edu/event_test.html
      Message 2 of 2 , Aug 29, 2008
      • 0 Attachment
        Oh, and since I forgot to include it, I've put a sample page up here at:

        http://catalog.wsu.edu/event_test.html
      Your message has been successfully submitted and would be delivered to recipients shortly.