Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of onreadystatechange events?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report