AJax¼¼Êõ

javascript ѧϰ(5)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-09-12 15:17 ÎÒÒªÆÀÂÛ( )

/** * Õâ¸öº¯Êý¾Í Ruby ÁË¡£ÎÒ¾õµÃËüµÄ×÷ÓÃÖ÷ÒªÓÐÁ½¸ö * 1. ´ó¸ÅÊÇ document.getElementById(id) µÄ×î¼ò»¯µ÷Óᣠ* ±ÈÈ磺$(aaa) ½«·µ»ØÉÏ aaa ¶ÔÏó * 2. µÃµ½¶ÔÏóÊý×é * ±ÈÈç: $(aaa,bbb) ·µ»ØÒ»¸ö°üÀ¨idΪaaaºÍbb

/**
 * Õâ¸öº¯Êý¾Í Ruby ÁË¡£ÎÒ¾õµÃËüµÄ×÷ÓÃÖ÷ÒªÓÐÁ½¸ö
 * 1.  ´ó¸ÅÊÇ document.getElementById(id) µÄ×î¼ò»¯µ÷Óá£
 * ±ÈÈ磺$("aaa") ½«·µ»ØÉÏ aaa ¶ÔÏó
 * 2.  µÃµ½¶ÔÏóÊý×é
 * ±ÈÈç: $("aaa","bbb") ·µ»ØÒ»¸ö°üÀ¨idΪ"aaa"ºÍ"bbb"Á½¸öinput¿Ø¼þ¶ÔÏóµÄÊý×é¡£
 */
function $() {
  var elements = new Array();
 
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;
     
    elements.push(element);
  }
 
  return elements;
}


Ajax::prototype Ô´Âë½â¶Á Ö® prototype.js ¶þ[תÔØ]
/**
 * ¶¨Òå Ajax ¶ÔÏó, ¾²Ì¬·½·¨ getTransport ·½·¨·µ»ØÒ»¸ö XMLHttp ¶ÔÏó
 */
var Ajax = {
  getTransport: function() {
    return Try.these(
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')},
      function() {return new XMLHttpRequest()}
    ) || false;
  },
 
  emptyFunction: function() {}
}

/**
 * ÎÒÒÔΪ´ËʱµÄAjax¶ÔÏóÆðµ½ÃüÃû¿Õ¼äµÄ×÷Óá£
 * Ajax.Base ÉùÃ÷Ϊһ¸ö»ù´¡¶ÔÏóÀàÐÍ
 * ×¢Òâ Ajax.Base ²¢Ã»ÓÐʹÓà Class.create() µÄ·½Ê½À´´´½¨£¬ÎÒÏëÊÇÒòΪ×÷Õß²¢²»Ï£Íû Ajax.Base ±»¿âʹÓÃÕßʵÀý»¯¡£
 * ×÷ÕßÔÚÆäËû¶ÔÏóÀàÐ͵ÄÉùÃ÷ÖУ¬½«»á¼Ì³ÐÓÚËü¡£
 * ¾ÍºÃÏñ java ÖеÄ˽ÓгéÏóÀà
 */
Ajax.Base = function() {};
Ajax.Base.prototype = {
  /**
   * extend (¼ûprototype.jsÖеĶ¨Òå) µÄÓ÷¨ÕæÊÇÈÃÈ˶úÄ¿Ò»ÐÂ
   * options Ê×ÏÈÉèÖÃĬÈÏÊôÐÔ£¬È»ºóÔÙ extend ²ÎÊý¶ÔÏó£¬ÄÇô²ÎÊý¶ÔÏóÖÐÒ²ÓÐͬÃûµÄÊôÐÔ£¬ÄÇô¾Í¸²¸ÇĬÈÏÊôÐÔÖµ¡£
   * ÏëÏëÈç¹ûÎÒдÕâÑùµÄʵÏÖ£¬Ó¦¸ÃÀàËÆÈçÏ£º
     setOptions: function(options) {
      this.options.methed = options.methed? options.methed : 'post';
      ..........
     }
     ÎÒÏëºÜ¶àʱºò£¬java ÏÞÖÆÁË js µÄ´´Òâ¡£
   */
  setOptions: function(options) {
    this.options = {
      method:       'post',
      asynchronous: true,
      parameters:   ''
    }.extend(options || {});
  }
}


/**
 * Ajax.Request ·â×° XmlHttp
 */
Ajax.Request = Class.create();

/**
 * ¶¨ÒåËÄÖÖʼþ(״̬)£¬ ²Î¿¼
 */
Ajax.Request.Events =
  ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];

/**
 *
 */
Ajax.Request.prototype = (new Ajax.Base()).extend({
  initialize: function(url, options) {
    this.transport = Ajax.getTransport();
    this.setOptions(options);
 
    try {
      if (this.options.method == 'get')
        url += '?' + this.options.parameters + '&_=';
   
     /**
      * ´Ë´¦ºÃÏñÇ¿ÖÆʹÓÃÁËÒì²½·½Ê½£¬¶ø²»ÊÇÒÀÕÕ this.options.asynchronous µÄÖµ
      */
      this.transport.open(this.options.method, url, true);
     
     /**
      * ÕâÀïÌṩÁË XmlHttp ´«Êä¹ý³ÌÖÐÿ¸ö²½ÖèµÄ»Øµ÷º¯Êý
      */
      if (this.options.asynchronous) {
        this.transport.onreadystatechange = this.onStateChange.bind(this);
        setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
      }
             
      this.transport.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
      this.transport.setRequestHeader('X-Prototype-Version', Prototype.Version);

      if (this.options.method == 'post') {
        this.transport.setRequestHeader('Connection', 'close');
        this.transport.setRequestHeader('Content-type',
          'application/x-www-form-urlencoded');
      }
     
      this.transport.send(this.options.method == 'post' ?
        this.options.parameters + '&_=' : null);
                     
    } catch (e) {
    }   
  },
     
  onStateChange: function() {
    var readyState = this.transport.readyState;
   /**
    * Èç¹û²»ÊÇ Loading ״̬£¬¾Íµ÷Óûص÷º¯Êý
     */
    if (readyState != 1)
      this.respondToReadyState(this.transport.readyState);
  },
 
  /**
   * »Øµ÷º¯Êý¶¨ÒåÔÚ this.options ÊôÐÔÖУ¬±ÈÈç:
      var option = {
         onLoaded : function(req) {...};
         ......
      }
      new Ajax.Request(url, option);
   */
  respondToReadyState: function(readyState) {
    var event = Ajax.Request.Events[readyState];
    (this.options['on' + event] || Ajax.emptyFunction)(this.transport);
  }
});

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Ajax ½Ì³Ì ÈëÊѧϰ

    Ajax ½Ì³Ì ÈëÊѧϰ

    2016-09-02 14:01

  • ѧϰjavascript:Å£È˵Ľ²×ùÊÓƵºÍPPT

    ѧϰjavascript:Å£È˵Ľ²×ùÊÓƵºÍPPT

    2016-08-22 13:00

  • ×îÈ«ÃæµÄJavaScriptµ÷ÊÔ¼¼ÇÉ×ܽá

    ×îÈ«ÃæµÄJavaScriptµ÷ÊÔ¼¼ÇÉ×ܽá

    2016-08-21 15:01

  • ѧϰjavawebʱµÄAjax±Ê¼Ç

    ѧϰjavawebʱµÄAjax±Ê¼Ç

    2016-08-18 11:00

ÍøÓѵãÆÀ
¹