Events
Next ❯jQuery Ajax
- turned_in_notEvents
- onloadstart - To define a event function, triggered at the start of the request
- onprogress - To define a event function, triggered while request in progress
- onabort - To define a event function, triggered when request is aborted
- onload - To define a event function, triggered when request is received
- onerror - To define a event function, triggered when any error occurred
- onloadend - To define a event function, triggered when request load ends
Example
<!DOCTYPE html>
<html>
<body>
<div id="text1"></div>
<button onclick="fun()">Load (ajax_demo.txt)</button>
<script>
function fun(){
var xhr = new XMLHttpRequest();
//Adding events to XMLHttpRequest object &
//Event must declare before sending the request
xhr.onloadstart = function(){alert("onloadstart event executed");}
xhr.onprogress = function(){alert("onprogress event executed");}
xhr.onabort = function(){alert("onabort event executed");}
xhr.onerror = function(){alert("onerror event executed");}
xhr.onload = function(){alert("onload event executed");}
xhr.onloadend = function(){alert("onloadend event executed");}
xhr.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
// here, handle the server response
document.getElementById("text1").innerHTML = this.responseText;
}
};
xhr.open("GET", "ajax_demo.txt", true);
xhr.send();
}
</script>
</body>
</html>
OUTPUT:❮ Prev Ajax Response
Next ❯jQuery Ajax