jquery多版本兼容方法

 框架使用的jq版本与上传文件的jq版本不一样

开发者技术变现资源聚集地

https://www.baiydu.com

/*
* jQuery 1.2.1 – New Wave Javascript

jQuery.noConflict()的存在只有一个目的:它允许你在同一个页面加载多个jQuery实例,尤其是不同版本的jQuery。你可能会觉得奇怪,为什么要在一个页面加载/使用多个不同版本的jQuery对象呢?一般而言,有两种情况。第一种情况,你的业务代码采用了最新版的jQuery库,而你选用的第三方插件依赖的更早版本的jQuery库;第二种情况,你正维护着一个系统,它已有的业务代码由于各种原因,引用了较老版本的jQuery库,你新开发的模块采用的是其他版本的jQuery库。不论哪种情况,你都不得不面对,jQuery对象/方法冲突的问题。幸运的是,jQuery.noConflict()帮你解决了这个烦恼。

*/

<script  type="text/javascript"  src="<%=basePath%>/resources/Js/jquery.js" ></script>  
<!--  <script type="text/javascript" src="<%=basePath%>/resources/Js/ajaxfileupload.js" > </script> -->
     <script type="text/javascript" >
      
      
      var $jq = jQuery.noConflict(true);

      $jq.extend({
           createUploadIframe: function(id, uri)
          {
                  //create frame
                  var frameId = \'jUploadFrame\' + id;
                  
                  if(window.ActiveXObject) {
                         if($jq.browser.version=="9.0" || $jq.browser.version=="10.0"){
                              var io = document.createElement(\'iframe\');
                              io.id = frameId;
                              io.name = frameId;
                          }else if($jq.browser.version=="6.0" || $jq.browser.version=="7.0" || $jq.browser.version=="8.0"){
                              var io = document.createElement(\'<iframe id="\' + frameId + \'" name="\' + frameId + \'" />\');
                              if(typeof uri== \'boolean\'){
                                  io.src = \'javascript:false\';
                              }
                              else if(typeof uri== \'string\'){
                                  io.src = uri;
                              }
                          }
                      }
                  else {
                      var io = document.createElement(\'iframe\');
                      io.id = frameId;
                      io.name = frameId;
                  }
                  io.style.position = \'absolute\';
                  io.style.top = \'-1000px\';
                  io.style.left = \'-1000px\';

                  document.body.appendChild(io);

                  return io            
          },
          createUploadForm: function(id, fileElementId,data)
          {
              //create form    
              var formId = \'jUploadForm\' + id;
              var fileId = \'jUploadFile\' + id;
              var form = $jq(\'<form  action="" method="POST" name="\' + formId + \'" id="\' + formId + \'" enctype="multipart/form-data"></form>\');    
              var oldElement = $jq(\'#\' + fileElementId);
              if(typeof(fileElementId) == \'string\'){
                  fileElementId = [fileElementId];
              }
              for(var i in fileElementId){
                  var oldElement = $jq(\'#\' + fileElementId[i]);
                  var newElement = $jq(oldElement).clone();
                  $jq(oldElement).attr(\'id\', fileId);
                  $jq(oldElement).before(newElement);
                  $jq(oldElement).appendTo(form);
              }
              //set attributes
              if (data) {
                  for ( var i in data) {
                      $jq(\'<input type="hidden" name="\' + i + \'" value="\' + data[i] + \'" />\').appendTo(form);
                  }
              }
              
              $jq(form).css(\'position\', \'absolute\');
              $jq(form).css(\'top\', \'-1200px\');
              $jq(form).css(\'left\', \'-1200px\');
              $jq(form).appendTo(\'body\');        
              return form;
          },

          ajaxFileUpload: function(s) {
              // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout        
              s = $jq.extend({}, $jq.ajaxSettings, s);
              var id = new Date().getTime()        
              var form = $jq.createUploadForm(id, s.fileElementId,s.data);
              var io = $jq.createUploadIframe(id, s.secureuri);
              var frameId = \'jUploadFrame\' + id;
              var formId = \'jUploadForm\' + id;        
              // Watch for a new set of requests
              if ( s.global && ! $jq.active++ )
              {
                  $jq.event.trigger( "ajaxStart" );
              }            
              var requestDone = false;
              // Create the request object
              var xml = {}   
              if ( s.global )
                  $jq.event.trigger("ajaxSend", [xml, s]);
              // Wait for a response to come back
              var uploadCallback = function(isTimeout)
              {            
                  var io = document.getElementById(frameId);
                  try 
                  {                
                      if(io.contentWindow)
                      {
                           xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
                           xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
                           
                      }else if(io.contentDocument)
                      {
                           xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
                          xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
                      }                        
                  }catch(e)
                  {
                      $jq.handleError(s, xml, null, e);
                  }
                  if ( xml || isTimeout == "timeout") 
                  {                
                      requestDone = true;
                      var status;
                      try {
                          status = isTimeout != "timeout" ? "success" : "error";
                          // Make sure that the request was successful or notmodified
                          if ( status != "error" )
                          {
                              // process the data (runs the xml through httpData regardless of callback)
                              var data = $jq.uploadHttpData( xml, s.dataType );    
                              // If a local callback was specified, fire it and pass it the data
                              if ( s.success )
                                  s.success( data, status );
          
                              // Fire the global callback
                              if( s.global )
                                  $jq.event.trigger( "ajaxSuccess", [xml, s] );
                          } else
                              $jq.handleError(s, xml, status);
                      } catch(e) 
                      {
                          status = "error";
                          $jq.handleError(s, xml, status, e);
                      }

                      // The request was completed
                      if( s.global )
                          $jq.event.trigger( "ajaxComplete", [xml, s] );

                      // Handle the global AJAX counter
                      if ( s.global && ! --$jq.active )
                          $jq.event.trigger( "ajaxStop" );

                      // Process result
                      if ( s.complete )
                          s.complete(xml, status);

                      $jq(io).unbind()

                      setTimeout(function()
                                          {    try 
                                              {
                                                  $jq(io).remove();
                                                  $jq(form).remove();    
                                                  
                                              } catch(e) 
                                              {
                                                  $jq.handleError(s, xml, null, e);
                                              }                                    

                                          }, 100)

                      xml = null

                  }
              }
              // Timeout checker
              if ( s.timeout > 0 ) 
              {
                  setTimeout(function(){
                      // Check to see if the request is still happening
                      if( !requestDone ) uploadCallback( "timeout" );
                  }, s.timeout);
              }
              try 
              {
                 // var io = $jq(\'#\' + frameId);
                  var form = $jq(\'#\' + formId);
                  $jq(form).attr(\'action\', s.url);
                  $jq(form).attr(\'method\', \'POST\');
                  $jq(form).attr(\'target\', frameId);
                  if(form.encoding)
                  {
                      form.encoding = \'multipart/form-data\';                
                  }
                  else
                  {                
                      form.enctype = \'multipart/form-data\';
                  }            
                  $jq(form).submit();

              } catch(e) 
              {            
                  $jq.handleError(s, xml, null, e);
              }
              if(window.attachEvent){
                  document.getElementById(frameId).attachEvent(\'onload\', uploadCallback);
              }
              else{
                  document.getElementById(frameId).addEventListener(\'load\', uploadCallback, false);
              }         
              return {abort: function () {}};    

          },

          uploadHttpData: function( r, type ) {
              var data = !type;
              data = type == "xml" || data ? r.responseXML : r.responseText;
              // If the type is "script", eval it in global context
              if ( type == "script" )
                  $jq.globalEval( data );
              // Get the JavaScript object, if JSON is used.
              if ( type == "json" ){
                  ////////////以下为新增代码///////////////  
                  data = r.responseText;  
                  var start = data.indexOf(">");  
                  if(start != -1) {  
                      var end = data.indexOf("<", start + 1);  
                      if(end != -1) {  
                        data = data.substring(start + 1, end);  
                      }  
                  }  
                  ///////////以上为新增代码///////////////  
                  eval( "data = " + data );
              // evaluate scripts within html
              }
              if ( type == "html" )
                  $jq("<div>").html(data).evalScripts();
                  //alert($jq(\'param\', data).each(function(){alert($jq(this).attr(\'value\'));}));
              return data;
          },
          handleError: function( s, xhr, status, e ) {
              // If a local callback was specified, fire it
                      if ( s.error ) {
                          s.error.call( s.context || s, xhr, status, e );
                      }

                      // Fire the global callback
                      if ( s.global ) {
                          (s.context ? $jq(s.context) : $jq.event).trigger( "ajaxError", [xhr, s, e] );
                      }
          }
      })


      
   
function uploadFile() { 
        var file = $jq("#inputFile").val();
        if (file != "" && file != null) {
            $jq.ajaxFileUpload({
                url: "http://localhost:8080/autoDrainageServlet/myQQNumberServlet",
                secureuri: false,
                //name:\'inputFile\',
                fileElementId: \'inputFile\',//file标签的id
                dataType: \'json\',//返回数据的类型
                global:true,
                data:{headCode:\'SumAmount\'},
                complete:function(){
                    $jq.messager.progress(\'close\');
                    $jq("#inputFile").val("");
                },
                success: function (data) {

                    if (data =="0") {
                        HdUtil.messager.info("表格数据导入成功");
                       // alert("上传成功");
                        queryCodShipData();

                    } else {
                        alert("上传失败");

                    }


                },

            });
    }
}
 </script>
 

/*
jquery-1.8.1
*/
<script type="text/javascript" src="<%=basePath%>/resources/Js/jquery-1.8.1.min.js"></script> <script type="text/javascript" src="<%=basePath%>/resources/Js/bui-min.js"></script>

 

版权声明:本文为xiaoliao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaoliao/p/10945339.html