加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何让ajaxfileupload.js支持IE9,IE10,并可以传递多个参数?

发布时间:2020-12-15 22:03:20 所属栏目:百科 来源:网络整理
导读:exception : SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5) 思路分析: 第一步:兼容IE9,firefox,Opera,Safari等浏览器; var iframe = document.createElement("iframe"); iframe.setAttribute("id","yui-history-iframe"); iframe.setAttribut

exception : SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5)

思路分析:
第一步:兼容IE9,firefox,Opera,Safari等浏览器;
var iframe = document.createElement("iframe");
iframe.setAttribute("id","yui-history-iframe");
iframe.setAttribute("src","../../images/defaults/transparent-pixel.gif");
iframe.setAttribute("style","position:absolute;top:0;left:0;width:1px;height:1px;visibility:hidden;")

第二步:兼容IE6-8:由于ie6-8 不能修改iframe的name属性
var oFrame = isIE ? document.createElement("<iframe name=/"" + this._FrameName + "/">") : document.createElement("iframe");
oFrame.name = "iframName";

1、如何让ajaxfileupload.js支持IE9、IE10?

打开ajaxfileupload 文件,找到下面的代码。

[javascript] view plain copy
  1. if(window.ActiveXObject){
  2. vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
  3. if(typeofuri=='boolean'){
  4. io.src='javascript:false';
  5. }
  6. elseif(typeofuri=='string'){
  7. io.src=uri;
  8. }
  9. }
修改成如下:
[javascript] view plain copy
  1. if(window.ActiveXObject){
  2. if(jQuery.browser.version=="9.0"||jQuery.browser.version=="10.0"){
  3. vario=document.createElement('iframe');
  4. io.id=frameId;
  5. io.name=frameId;
  6. }elseif(jQuery.browser.version=="6.0"||jQuery.browser.version=="7.0"||jQuery.browser.version=="8.0"){
  7. vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
  8. if(typeofuri=='boolean'){
  9. io.src='javascript:false';
  10. }
  11. elseif(typeofuri=='string'){
  12. io.src=uri;
  13. }
  14. }
  15. }

2、如何让ajaxfileupload.js可以在文件上传的同时传递多个台数。

找到以下代码:

[javascript] view plain copy
  1. ajaxFileUpload:function(s){
  2. //TODOintroduceglobalsettings,allowingtheclienttomodifythemforallrequests,notonlytimeout
  3. s=jQuery.extend({},jQuery.ajaxSettings,s);
  4. varid=newDate().getTime()
  5. varform=jQuery.createUploadForm(id,s.fileElementId);
增加自己要传递的参数:
[javascript] view plain copy
  1. ajaxFileUpload:function(s){
  2. //TODOintroduceglobalsettings,s.fileElementId,s.tag_name,s.tag_link,s.tag_sort,s.tag_status,s.tag_id);
这里我们增加了五个传递参数。s.tag_name,s.tag_id

接着找到:

[javascript] view plain copy
  1. createUploadForm:function(id,fileElementId,tag_name,tag_link,tag_sort,tag_status,tag_id)//增加tag_name,tag_id
  2. {
  3. //createform
  4. varformId='jUploadForm'+id;
  5. varfileId='jUploadFile'+id;
  6. //--增加以下内容
  7. vartagNameId='tag_name'+id;
  8. vartagLinkId='tag_link'+id;
  9. vartagSortId='tag_sort'+id;
  10. vartagStatusId='tag_status'+id;
  11. vartagIdId='tag_id'+id;
  12. //--end
  13. varform=$('<formaction=""method="POST"name="'+formId+'"id="'+formId+'"enctype="multipart/form-data"></form>');
  14. varoldElement=$('#'+fileElementId);
  15. varnewElement=$(oldElement).clone();
  16. //--增加以下内容
  17. vartagNameElement='<inputtype="text"name="tag_name"value="'+tag_name+'">';
  18. vartagLinkElement='<inputtype="text"name="tag_link"value="'+tag_link+'">';
  19. vartagSortElement='<inputtype="text"name="tag_sort"value="'+tag_sort+'">';
  20. vartagStatusElement='<inputtype="text"name="tag_status"value="'+tag_status+'">';
  21. vartagIdElement='<inputtype="text"name="tag_id"value="'+tag_id+'">';
  22. //--end
  23. $(oldElement).attr('id',fileId);
  24. $(oldElement).before(newElement);
  25. $(oldElement).appendTo(form);
  26. //--增加以下的内容
  27. $(tagNameElement).appendTo(form);
  28. $(tagLinkElement).appendTo(form);
  29. $(tagSortElement).appendTo(form);
  30. $(tagStatusElement).appendTo(form);
  31. $(tagIdElement).appendTo(form);
  32. //--end
  33. //setattributes
  34. $(form).css('position','absolute');
  35. $(form).css('top','-1200px');
  36. $(form).css('left','-1200px');
  37. $(form).appendTo('body');
  38. returnform;
  39. },
注意注释中的内容为增加了内容。

修改完后,如何使用?

[javascript] view plain copy
  1. $.ajaxFileUpload({
  2. url:web_url,
  3. secureuri:false,
  4. //以下为增加的传递参数
  5. tag_name:tag_name,
  6. tag_link:tag_link,
  7. tag_sort:tag_sort,
  8. tag_status:tag_status,
  9. tag_id:tag_id,
  10. //--end
  11. fileElementId:result[0],
  12. dataType:'json',
  13. success:function(data,status){}
  14. //以下省略

OK,done.


附ajaxfileupload完整版:

[javascript] view plain copy
  1. jQuery.extend({
  2. createUploadIframe:function(id,uri)
  3. {
  4. //createframe
  5. varframeId='jUploadFrame'+id;
  6. if(window.ActiveXObject){
  7. //vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
  8. //if(typeofuri=='boolean'){
  9. //io.src='javascript:false';
  10. //}
  11. //elseif(typeofuri=='string'){
  12. //io.src=uri;
  13. //}
  14. //fixie9andie10-------------
  15. if(jQuery.browser.version=="9.0"||jQuery.browser.version=="10.0"){
  16. vario=document.createElement('iframe');
  17. io.id=frameId;
  18. io.name=frameId;
  19. }elseif(jQuery.browser.version=="6.0"||jQuery.browser.version=="7.0"||jQuery.browser.version=="8.0"){
  20. vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
  21. if(typeofuri=='boolean'){
  22. io.src='javascript:false';
  23. }
  24. elseif(typeofuri=='string'){
  25. io.src=uri;
  26. }
  27. }
  28. }
  29. else{
  30. vario=document.createElement('iframe');
  31. io.id=frameId;
  32. io.name=frameId;
  33. }
  34. io.style.position='absolute';
  35. io.style.top='-1000px';
  36. io.style.left='-1000px';
  37. document.body.appendChild(io);
  38. returnio
  39. },
  40. createUploadForm:function(id,tag_id)
  41. {
  42. //createform
  43. varformId='jUploadForm'+id;
  44. varfileId='jUploadFile'+id;
  45. //--
  46. vartagNameId='tag_name'+id;
  47. vartagLinkId='tag_link'+id;
  48. vartagSortId='tag_sort'+id;
  49. vartagStatusId='tag_status'+id;
  50. vartagIdId='tag_id'+id;
  51. //--end
  52. varform=$('<formaction=""method="POST"name="'+formId+'"id="'+formId+'"enctype="multipart/form-data"></form>');
  53. varoldElement=$('#'+fileElementId);
  54. varnewElement=$(oldElement).clone();
  55. //--
  56. vartagNameElement='<inputtype="text"name="tag_name"value="'+tag_name+'">';
  57. vartagLinkElement='<inputtype="text"name="tag_link"value="'+tag_link+'">';
  58. vartagSortElement='<inputtype="text"name="tag_sort"value="'+tag_sort+'">';
  59. vartagStatusElement='<inputtype="text"name="tag_status"value="'+tag_status+'">';
  60. vartagIdElement='<inputtype="text"name="tag_id"value="'+tag_id+'">';
  61. //--end
  62. $(oldElement).attr('id',fileId);
  63. $(oldElement).before(newElement);
  64. $(oldElement).appendTo(form);
  65. //--
  66. $(tagNameElement).appendTo(form);
  67. $(tagLinkElement).appendTo(form);
  68. $(tagSortElement).appendTo(form);
  69. $(tagStatusElement).appendTo(form);
  70. $(tagIdElement).appendTo(form);
  71. //--end
  72. //setattributes
  73. $(form).css('position',
  74. ajaxFileUpload:function(s){
  75. //TODOintroduceglobalsettings,s.tag_id);
  76. vario=jQuery.createUploadIframe(id,s.secureuri);
  77. varframeId='jUploadFrame'+id;
  78. varformId='jUploadForm'+id;
  79. //Watchforanewsetofrequests
  80. if(s.global&&!jQuery.active++)
  81. {
  82. jQuery.event.trigger("ajaxStart");
  83. }
  84. varrequestDone=false;
  85. //Createtherequestobject
  86. varxml={}
  87. if(s.global)
  88. jQuery.event.trigger("ajaxSend",[xml,s]);
  89. //Waitforaresponsetocomeback
  90. varuploadCallback=function(isTimeout)
  91. {
  92. vario=document.getElementById(frameId);
  93. try
  94. {
  95. if(io.contentWindow)
  96. {
  97. xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  98. xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  99. }elseif(io.contentDocument)
  100. {
  101. xml.responseText=io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  102. xml.responseXML=io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  103. }
  104. }catch(e)
  105. {
  106. jQuery.handleError(s,xml,null,e);
  107. }
  108. if(xml||isTimeout=="timeout")
  109. {
  110. requestDone=true;
  111. varstatus;
  112. try{
  113. status=isTimeout!="timeout"?"success":"error";
  114. //Makesurethattherequestwassuccessfulornotmodified
  115. if(status!="error")
  116. {
  117. //processthedata(runsthexmlthroughhttpDataregardlessofcallback)
  118. vardata=jQuery.uploadHttpData(xml,s.dataType);
  119. //Ifalocalcallbackwasspecified,fireitandpassitthedata
  120. if(s.success)
  121. s.success(data,status);
  122. //Firetheglobalcallback
  123. if(s.global)
  124. jQuery.event.trigger("ajaxSuccess",s]);
  125. }else
  126. jQuery.handleError(s,status);
  127. }catch(e)
  128. {
  129. status="error";
  130. jQuery.handleError(s,status,e);
  131. }
  132. //Therequestwascompleted
  133. if(s.global)
  134. jQuery.event.trigger("ajaxComplete",s]);
  135. //HandletheglobalAJAXcounter
  136. if(s.global&&!--jQuery.active)
  137. jQuery.event.trigger("ajaxStop");
  138. //Processresult
  139. if(s.complete)
  140. s.complete(xml,status);
  141. jQuery(io).unbind()
  142. setTimeout(function()
  143. {try
  144. {
  145. $(io).remove();
  146. $(form).remove();
  147. }catch(e)
  148. {
  149. jQuery.handleError(s,e);
  150. }
  151. },100)
  152. xml=null
  153. }
  154. }
  155. //Timeoutchecker
  156. if(s.timeout>0)
  157. {
  158. setTimeout(function(){
  159. //Checktoseeiftherequestisstillhappening
  160. if(!requestDone)uploadCallback("timeout");
  161. },s.timeout);
  162. }
  163. try
  164. {
  165. //vario=$('#'+frameId);
  166. varform=$('#'+formId);
  167. $(form).attr('action',s.url);
  168. $(form).attr('method','POST');
  169. $(form).attr('target',frameId);
  170. if(form.encoding)
  171. {
  172. form.encoding='multipart/form-data';
  173. }
  174. else
  175. {
  176. form.enctype='multipart/form-data';
  177. }
  178. $(form).submit();
  179. }catch(e)
  180. {
  181. jQuery.handleError(s,e);
  182. }
  183. if(window.attachEvent){
  184. document.getElementById(frameId).attachEvent('onload',uploadCallback);
  185. }
  186. else{
  187. document.getElementById(frameId).addEventListener('load',uploadCallback,false);
  188. }
  189. return{abort:function(){}};
  190. },
  191. uploadHttpData:function(r,type){
  192. vardata=!type;
  193. data=type=="xml"||data?r.responseXML:r.responseText;
  194. //Ifthetypeis"script",evalitinglobalcontext
  195. if(type=="script")
  196. jQuery.globalEval(data);
  197. //GettheJavaScriptobject,ifJSONisused.
  198. if(type=="json")
  199. eval("data="+data);
  200. //evaluatescriptswithinhtml
  201. if(type=="html")
  202. jQuery("<div>").html(data).evalScripts();
  203. //alert($('param',data).each(function(){alert($(this).attr('value'));}));
  204. returndata;
  205. }
  206. })
转载:http://www.52php.cn/article/p-xjquddto-gy.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读