Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of onreadystatechange events?

Shulou Source: shulou.com Published: 2022-06-03 11:02:07 09月11日 Update

This article will explain in detail the usefulness of the onreadystatechange incident for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Onreadystatechange event, when the request is sent to the server, we need to perform some response-based tasks. Whenever the readyState changes, the onreadystatechange event is triggered.

What is the onreadystatechange property?

The readyState property stores the status information of the XMLHttpRequest.

Here are three important properties of the XMLHttpRequest object:

Attribute description

Onreadystatechange stores the function (or function name), which is called whenever the readyState property changes.

ReadyState

The state of having a XMLHttpRequest. It changes from 0 to 4.

0: request is not initialized

1: the server connection has been established

2: the request has been received

3: request processing

4: the request has been completed and the response is ready

Status

"OK"

404: page not found

In the onreadystatechange event, we specify the task to be performed when the server responds that it is ready to be processed.

When readyState equals 4 and the status is 200, the response is ready:

Xmlhttp.onreadystatechange=function ()

{

If (xmlhttp.readyState==4&&xmlhttp.status==200)

{

Document.getElementById ("myDiv") [xss_clean] = xmlhttp.responseText

}

}

Note: the onreadystatechange event is triggered 5 times (0-4), corresponding to each change in the readyState.

Onreadystatechange uses the Callback function

A callback function is a function that is passed to another function as an argument.

If there are multiple AJAX tasks on your site, you should write a standard function for creating XMLHttpRequest objects and call that function for each AJAX task.

The function call should contain the URL and the tasks performed when the onreadystatechange event occurs (each call may be different):

FunctionmyFunction ()

{

LoadXMLDoc ("ajax_info.txt", function ()

{

If (xmlhttp.readyState==4&&xmlhttp.status==200)

{

Document.getElementById ("myDiv") [xss_clean] = xmlhttp.responseText

}

})

}

This is the end of this article on "what's the use of the onreadystatechange incident?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Functions events tasks properties servers states articles services objects more changes processing good practical same important three information content parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Linux Xiaomi MySQL MariaDB