需要简化javascript函数(也可以使用jquery)



我有一个很长的函数,可以使用隐藏的iFrame上传照片,它工作得很好,而且一切都很好,问题是它有很多混乱的代码,我想制作一个只有几行的干净函数,这样可以更容易地使用更少的代码。

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"});
*/
}

好吧,这里有几行代码应该在新函数中定义,我对代码块进行了/**/注释,并对该块进行了操作。

开始:

function fileUpload(a, b) {
var c = document.createElement("iframe");
c.setAttribute("id", "upload_iframe"), c.setAttribute("name", "upload_iframe"), c.setAttribute("width", "0"), c.setAttribute("height", "0"), c.setAttribute("border", "0"), c.setAttribute("style", "width: 0; height: 0; border: none;"), a.parentNode.appendChild(c), window.frames.upload_iframe.name = "upload_iframe", iframeId = document.getElementById("upload_iframe");
var d = function () {
    iframeId.detachEvent ? iframeId.detachEvent("onload", d) : iframeId.removeEventListener("load", d, !1), iframeId.contentDocument ? content = iframeId.contentDocument.body.innerHTML : iframeId.contentWindow ? content = iframeId.contentWindow.document.body.innerHTML : iframeId.document && (content = iframeId.document.body.innerHTML), setTimeout("iframeId.parentNode.removeChild(iframeId)", 250)
};
iframeId.addEventListener && iframeId.addEventListener("load", d, !0), iframeId.attachEvent && iframeId.attachEvent("onload", d), a.setAttribute("target", "upload_iframe"), a.setAttribute("action", b), a.setAttribute("method", "post"), a.setAttribute("enctype", "multipart/form-data"), a.setAttribute("encoding", "multipart/form-data"), a.submit()
}

Iframe方法是"被否决的";int 2.0网络时代。

请忘记使用innerHTML和其他非标准方式。

如果你打算使用Jquery,这个任务会更简单高效。

在这里你可以看到它看到这个演示Jquery上传文件演示

在这里你可以得到参考资料和如何Jquery上传文档

一种可能性是创建一个包含所有公共功能的基类,然后创建一个新的类,该类从中扩展,其中包含更具体的功能。

例如。http://ejohn.org/blog/simple-javascript-inheritance/

最新更新