Prototype - AJAX Responders() 方法

AJAX Ajax.Responders 允许您注册有关基于 Prototype 的 AJAX 请求的每个步骤的全局侦听器。

有两个 Responder,一个用于注册侦听器,另一个可用于取消注册侦听器。

语法

Ajax.Responders.register(responder);

Ajax.Responders.unregister(responder);

返回值

不适用。

取消注册响应器

如果您打算取消注册响应器,请务必先定义它,然后将引用传递给register,最后,在时间到时,取消注册

示例

以下是通过监视 onCreate 和 onComplete 事件来计数当前活动的 AJAX 请求的示例。

多次单击提交按钮,然后查看结果 −

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function SubmitRequest() {
            new Ajax.Request('/cgi-bin/ajax.cgi', {
               method: 'get',
               onSuccess: successFunc  
            });
         }
         Ajax.Responders.register({
            onCreate: function() {
               var count = Ajax.activeRequestCount++;
               var container = $('requests');
               container.update(count);
            },
            onComplete: function() {
               var count =  Ajax.activeRequestCount--;
               var container = $('requests');
               container.update(count);
            }
         });
         function successFunc(response) {
            var container = $('notice');
            var content = response.responseText;
            container.update(content);
         }
      </script>
   </head>

   <body>
      <p>Click Submit button many times and see the result.</p>
      <br />
 
      <div id = "notice">Current Notice</div>
      <br />
      <div id = "requests">Current Request</div>
      <br />
      <input type = "button" value = "Submit" onclick = "SubmitRequest();"/>
   </body>
</html>

这是ajax.cgi的内容>

#!/usr/bin/perl

print "Content-type: text/html

";

print "This content is returned by AJAX cgi 
"; print "Current Time " . localtime;

输出

prototype_ajax_tutorial.html