All pastes #1507458 Raw Edit

matiu

public javascript v1 · immutable
#1507458 ·published 2009-07-26 01:44 UTC
rendered paste body
function uploadForm(form) {    var form = $(form);    if (form.get('tag') != "form") return false    var iFrame = new Element("iframe", {      "id": "fileUpload",      "name": "fileUpload",      "styles": { "display": "none" }    });    form.adopt(iFrame);    window.frames["fileUpload"].name = "fileUpload";    iFrame.addEvent("load", function() {      iFrame.removeEvent("load", arguments.callee)      var contents = $(iFrame).contentDocument.body.innerHTML;      if (contents && contents.substring(0,1) == '<') {          /* See if there's already a sub fieldSet in the form, if there is, we'll use that */          var contentsFieldDiv = $(iFrame.contentDocument.body).getElement('div.fields');          if (contentsFieldDiv) {            contents = $(contentsFieldDiv).get('html');          } else {              var fieldsets = iFrame.contentDocument.body.getElementsByTagName('fieldset');              if (fieldsets.length > 0) {                contents = $(fieldsets.item(0)).get('html');              }          }          /* Contents is the new form contents */          /* Replace either the 'fields div', or the 'fieldset' element */          var fieldDiv = $(form.getElement('div.fields'));          if (fieldDiv) {              $(fieldDiv).set('html', contents);          } else {              var fieldset = $(form.getElement('fieldset'));              $(fieldset).set('html', contents);          }          /* Run any scripts in there */          var scripts = iFrame.contentDocument.body.getElementsByTagName('script');          for (var i=0; i<scripts.length; i++) {            var scriptEle = $(scripts.item(i));            eval(scriptEle.get('text'));          }      } else if (contents) {          /* Contents is JS for evaling */          eval(contents);      }      form.removeChild.delay(20, form, iFrame)    });    $(form).setProperties({      "target": "fileUpload",      "enctype": "multipart/form-data",      "encoding": "multipart/form-data",      "method": "post"    });    return form;}$('cruiseForm').addEvent('submit', function (evt) { uploadForm(evt.target) });$('itineraryForm').addEvent('submit', function (evt) { uploadForm(evt.target) });