Prototype Element Update Confusion

Posted on August 5, 2009

0


At work we are currently working on reimplementing a piece of software called SNEEP. SNEEP is a plugin for repository software called EPrints which is developed at the University of Southampton. According to the Registry of Open Access Repositories (ROAR) EPrints currently has 333 installations around the world as of 6th August 2009. As part of the SNEEP plugin we have been adding in the ability to edit comments and notes using AJAX meaning that users don’t have to leave the deposit page to author their comments or notes.

EPrints comes with the Prototype and Scriptaculous JavaScript libraries installed so we had to use these as opposed to my regular choice of jQuery. Owing to the static approach used for generating the document views in EPrints we had to inject the whole comments section of the document using AJAX. We did this by having the server generate the DOM and then sending it back to the browser which injects it into the page. This way the page can dynamically load the comments box depending on if a user is logged in or not.

Initially we managed to get the code running with Firefox and Safari but we had no joy with the code in IE. Here is some of the code that was originally used.

function sneepCommentFormInit(transport) {
  // Load in the HTML generated by the server side script
  $('ep_sneep_main').update(transport.responseText);

  // If the server sent back a form so the user can write a new comment
  // then we need to remove the old submit button and add one which is
  // attached to one of our JavaScript functions.
  $$(".ep_sneep_comment_form_container").each(function(element) {
    var submitButton = document.createElement("input");
    submitButton.writeAttribute("type", "button");
    submitButton.writeAttribute("id", "sneep_comment_submit");
    submitButton.writeAttribute("value", $("sneep_comment_submit").readAttribute("value"));
    submitButton.observe('click', sneepCommentFormSubmit);
    $("sneep_comment_submit").remove();
    element.appendChild(submitButton);
    var inputcallback = document.createElement("input");
    inputcallback.writeAttribute("type", "hidden");
    inputcallback.writeAttribute("name", "callback");
    inputcallback.writeAttribute("value", "1");
    element.appendChild(inputcallback);
  });
}

Event.observe(window, 'load', function() {
  var rm = window.location.href.match(/(https?:\/\/.*)\/(\d+)\/?/);
  if (rm != null) {
    new Ajax.Request(rm[1]+'/cgi/sneep/view_sneep?eprintid='+rm[2]+'&type=comment', {
      method: 'get',
      onCreate: function() {
        // Put up a loading message
      },
      onComplete: sneepCommentFormInit
    });
  }
});

The problem appeared to be that IE does not finish loading the updated content before attempting to add the hidden field and attaching a new submit button which calls our client side method rather than allowing the form to submit straight back to the server.

The solution we chose for this issue was to modify the initial on load function in the following way;

Event.observe(window, 'load', function() {
  var rm = window.location.href.match(/(https?:\/\/.*)\/(\d+)\/?/);
  if (rm != null) {
    new Ajax.Request(rm[1]+'/cgi/sneep/view_sneep?eprintid='+rm[2]+'&type=comment', {
      method: 'get',
      onCreate: function() {
        // Put up a loading message
      },
      onComplete: function(transport) {
        $('ep_sneep_main').update(transport.responseText+
          "<script type=\"text/javascript\">"+
          "sneepCommentFormInit();"+
          "</script>");
      }
    });
  }
});

Basically in this code we removed the code from sneepCommentFormInit() that handles the returned text and put it inside the anonymous onComplete method. We also appended some JavaScript that gets executed as soon as it is evaluated. This means that the form initialization code is only run once all of the HTML has been interpreted and loaded meaning that IE now has the submit intercept function correctly attached.

Advertisements
Posted in: eprints, javascript