javascript - JSLint - 如何修复 ActiveXObject 在定义之前使用的错误

标签 javascript jslint

JSLint 对象指向第 22 行:

function() { return new ActiveXObject('Msxml2.XMLHTTP'); },

在 reqTry 数组中。有什么方法可以更改代码来防止 JSLint 中出现此错误吗?

它也反对下一行。

function Ajax() {
  this.req = null;
  this.url = null;
  this.status = null;
  this.statusText = '';
  this.method = 'GET';
  this.async = true;
  this.dataPayload = null;
  this.readyState = null;
  this.responseText = null;
  this.responseXML = null;
  this.handleResp = null;
  this.responseFormat = 'text'; // 'text', 'xml', 'object'
  this.mimeType = null;
  this.headers = [];


  this.init = function() {
    var i = 0;
    var reqTry = [
      function() { return new XMLHttpRequest(); },
      function() { return new ActiveXObject('Msxml2.XMLHTTP'); },
      function() { return new ActiveXObject('Microsoft.XMLHTTP' ); } ];

    while (!this.req && (i < reqTry.length)) {
      try {
        this.req = reqTry[i++]();
      }
      catch(e) {}
    }
    return true;
  };
  this.doGet = function(url, hand, format) {
    this.url = url;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.doReq();
  };
  this.doPost = function(url, dataPayload, hand, format) {
    this.url = url;
    this.dataPayload = dataPayload;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.method = 'POST';
    this.doReq();
  };
  this.doReq = function() {
    var self = null;
    var req = null;

    if (!this.init()) {
      alert('Could not create XMLHttpRequest object.');
      return;
    }
    req = this.req;
    req.open(this.method, this.url, this.async);
    if (this.method == "POST") {
      this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    if (this.method == 'POST') {
      req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    self = this;
    req.onreadystatechange = function() {
      var resp = null;
      self.readyState = req.readyState;
      if (req.readyState == 4) {

        self.status = req.status;
        self.statusText = req.statusText;
        self.responseText = req.responseText;
        self.responseXML = req.responseXML;

        switch(self.responseFormat) {
          case 'text':
            resp = self.responseText;
            break;
          case 'xml':
            resp = self.responseXML;
            break;
          case 'object':
            resp = req;
            break;
        }

        if (self.status > 199 && self.status < 300) {
          if (!self.handleResp) {
            alert('No response handler defined ' +
              'for this XMLHttpRequest object.');
            return;
          }
          else {
            self.handleResp(resp);
          }
        }

        else {
          self.handleErr(resp);
        }
      }
    };
    req.send(this.dataPayload);
  };
  this.abort = function() {
    if (this.req) {
      this.req.onreadystatechange = function() { };
      this.req.abort();
      this.req = null;
    }
  };
  this.handleErr = function() {
    var errorWin;
    // Create new window and display error
    try {
      errorWin = window.open('', 'errorWin');
      errorWin.document.body.innerHTML = this.responseText;
    }
    // If pop-up gets blocked, inform user
    catch(e) {
      alert('An error occurred, but the error message cannot be' +
      ' displayed because of your browser\'s pop-up blocker.\n' +
      'Please allow pop-ups from this Web site.');
    }
  };
  this.setMimeType = function(mimeType) {
    this.mimeType = mimeType;
  };
  this.setHandlerResp = function(funcRef) {
    this.handleResp = funcRef;
  };
  this.setHandlerErr = function(funcRef) {
    this.handleErr = funcRef;
  };
  this.setHandlerBoth = function(funcRef) {
    this.handleResp = funcRef;
    this.handleErr = funcRef;
  };
  this.setRequestHeader = function(headerName, headerValue) {
    this.headers.push(headerName + ': ' + headerValue);
  };

}

最佳答案

在 JavaScript 中,全局变量实际上是全局对象的属性。在浏览器中,全局对象是window。所以你可以写: window.ActiveXObject

关于javascript - JSLint - 如何修复 ActiveXObject 在定义之前使用的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15055528/

相关文章:

javascript - 如何检测浏览器是否支持window.close?

javascript - getElementsByTagName(a) 用于自动滚动

javascript - 类型错误 : undefined is not an object (evaluating 'store.getState' )

javascript - 添加到javascript中复杂类型的数组

JavaScript: 'Disallow leading _ in identifiers' 作为 JSLint 中的一个选项

javascript - 将当前元素作为参数传递给循环内的回调函数

javascript - onclick 和 href 一起不起作用

javascript - JSLint:设置控制台时出现只读错误,即使它被设置为可写全局

javascript - 放;在函数定义的末尾

javascript - JSlint 错误 'Don' t 在循环内创建函数。引发关于 Javascript 本身的问题