我有一个很长的功能,用于使用隐藏的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"});
*/
}
好的,所以这里有几行代码应该在新函数中定义,我有/ ** /对代码块进行了注释,并且对该块进行了操作。
答案 0 :(得分:0)
一种可能性是创建一个包含所有常用功能的基类,然后创建一个新的类,该类从中扩展出来,具有更多特定的功能。
答案 1 :(得分:0)
iframe方法,它已被“弃用”于2.0网络时代。
请忘记innerHTML和其他不标准的方式。
如果您打算使用Jquery,那么任务可以更简单有效。
在这里你可以看到它看到这个演示 Jquery Upload File Demo
在这里你得到了参考和方法 Jquery Upload Documentation
答案 2 :(得分:0)
你走了:
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()
}