Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't recommend using this library, first of all, since there are much better ones out there, but your question was how this one worked, not whether it was a good one or not.</p> <p>Here goes:</p> <ol> <li><code>ajaxUpload</code> is called by one of the buttons in the upload form.</li> <li><code>ajaxUpload</code> first <strong>ensures that all the function arguments are correct</strong>, creating an alert box (!) if there's a problem with one of them. (It would be a better idea to throw an error using <code>throw "error message"</code> instead.)</li> <li><code>ajaxUpload</code> then <strong>creates an iframe</strong> using <code>document.createElement</code>, and gives it zero width and height so it'll be invisible, before adding it to the document. It <strong>gives the iframe the name <code>ajax-temp</code></strong>.</li> <li><code>ajaxUpload</code> adds <strong>the <code>doUpload</code> function as a <code>onload</code> listener</strong> on the iframe. It uses the <code>addEvent</code> function as a cross-browser way to do this.</li> <li>Next, <code>ajaxUpload</code> <strong>sets the calling form's <code>target</code> attribute</strong> to point to the iframe, using the name that it gave the iframe earlier. This causes the form to submit into the iframe, instead of replacing the current page. The function also sets some other attributes on the form, even though it would probably be a better idea to add those attributes in the HTML source and not from Javascript.</li> <li>Finally, <code>ajaxUpload</code> <strong>submits the form</strong> using <code>form.submit()</code>.</li> <li>The browser submits the form to the server, causing <code>ajaxupload.php</code> to run.</li> <li>In <code>ajaxupload.php</code>, the script first checks that the browser did actually send a file (in case a curious user visited the url manually instead of submitting the form), then <strong>calls the <code>uploadImage</code> function</strong> with a bunch of parameters that it got from the <code>$_REQUEST</code> object.</li> <li><code>uploadImage</code> does a bunch of checks on the arguments it receives from the client, making sure that the file isn't too big, has the right extension, etc. If all the checks are passed, it <strong>saves the image</strong> and returns the filename where the image was saved. Otherwise it returns the list of errors.</li> <li>If the result returned by <code>uploadImage</code> isn't an array of errors, the script <strong>returns a success message</strong> and an <code>&lt;img&gt;</code> tag with it's <code>src</code> set to the url of the image. Otherwise, it returns a failure message.</li> <li>On the client, the return of the success or error message causes the <strong><code>load</code> event on the iframe to fire</strong>. The <code>doUpload</code> function, which is registered as a listener, <strong>is called</strong>.</li> <li><code>doUpload</code> removes itself as a listener from the iframe, and runs some javascript code in the context of the iframe by setting the iframe's src attribute. This code <strong>copies the iframe's contents</strong> to the parent page.</li> <li>Finally, <code>doUpload</code> <strong>removes the iframe</strong> from the document, waiting a short time beforehand in some browsers.</li> </ol> <p>Whew, that sure took a while to write.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload