Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to simplify javascript function (also using jquery)
    primarykey
    data
    text
    <p>I have a bit of a long function for uploading photos using hidden iFrames, it works great and everything, the thing is it's a lot of messy code, and I would like to make a clean function that only has a few rows or such for making it easier and less code to work with. </p> <pre><code>function fileUpload(form, action_url) { // Create the iframe... var iframe = document.createElement("iframe"); iframe.setAttribute("id","upload_iframe"); iframe.setAttribute("name","upload_iframe"); iframe.setAttribute("width","0"); iframe.setAttribute("height","0"); iframe.setAttribute("border","0"); iframe.setAttribute("style","width: 0; height: 0; border: none;"); // Add to document... form.parentNode.appendChild(iframe); window.frames['upload_iframe'].name="upload_iframe"; iframeId = document.getElementById("upload_iframe"); // Add event... var eventHandler = function() { if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler); else iframeId.removeEventListener("load", eventHandler, false); // Message from server... if (iframeId.contentDocument) { content = iframeId.contentDocument.body.innerHTML; } else if (iframeId.contentWindow) { content = iframeId.contentWindow.document.body.innerHTML; } else if (iframeId.document) { content = iframeId.document.body.innerHTML; } if(content) { /* THIS CODE SHOULD BE DEFINED IN NEW FUNCTION, AND PLACED HER AUTOMATICALLY- onSuccess() $('img.profile-picture').attr("src","photos/"+content+"_200.jpg"); $('.post-photo'+cookie('login')).attr("src","photos/"+content+"_55.jpg"); $(".add-photo-loading").fadeOut("fast"); */ } else { /*THIS SHOULD ALSO BE DEFINED IN THE NEW FUNCTION - onError() $(".add-photo-loading").html("Invalid Filetype"); $(".add-photo-loading").attr("class","upload-error"); */ } // Del the iframe... setTimeout('iframeId.parentNode.removeChild(iframeId)', 250); } if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true); if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler); // Set properties of form... form.setAttribute("target","upload_iframe"); form.setAttribute("action", action_url); form.setAttribute("method","post"); form.setAttribute("enctype","multipart/form-data"); form.setAttribute("encoding","multipart/form-data"); // Submit the form... form.submit(); /*THIS SHOULD BE FIRED IMMEDIATELY WHEN THIS FUNCTION IS CALLED BUT THIS CODE SHOULD ALSO BE FIRED ELSEWHERE $(".upload-error").attr("class","add-photo-loading"); $(".add-photo-loading").html("Uploading..."); $(".add-photo-loading").css({"display":"block"}); */ } </code></pre> <p>Okay, so there are a few lines of code in here that should be defined in the new function and I have /**/ commented around the blocks of code, with the action that is for that block.</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. 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