JAX Ajax.Responders允许您注册有关基于原型的 AJAX 请求的每一步的全局侦听器。
有两个响应器,一个用于注册监听器,另一个可用于注销监听器。
语法
Ajax.Responders.register(responder);
Ajax.Responders.unregister(responder);
返回值
那。
注销响应者
如果您计划注销响应程序,请务必先定义它,然后将引用传递给register,最后,在适当的时候,传递给unregister。
例子
以下示例通过监视 onCreate 和 onComplete 事件来计算当前活动的 AJAX 请求。
多次点击提交按钮,然后查看结果:
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "https://cdn.bootcdn.net/ajax/libs/prototype/1.7.3/prototype.min.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\n\n";
print "This content is returned by AJAX cgi
";
print "Current Time " . localtime;
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8298.html