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 compatibility between the creation of ajax core XMLHTTPRequest objects and browsers

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

Share

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

Today, I will talk to you about the compatibility between the creation of ajax core XMLHTTPRequest objects and browsers, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

MLHttpRequest object is the core of AJAX function. To develop AJAX program, we must start with understanding XMLHttpRequest object.

To understand XMLHttpRequest objects, start by creating XMLHttpRequest objects, using different methods to create XMLHttpRequest objects in different browsers:

First take a look at IE's method of creating a XMLHttpRequest object (method 1):

Var xmlhttp=ActiveXobject ("Msxml12.XMLHTTP"); / / newer versions of IE create Msxml12.XMLHTTP objects

Var xmlhttp=ActiveXobject ("Microsoft.XMLHTTP"); / / older versions of IE create Microsoft.XMLHTTP objects

Mozilla, Opera, Safari, and most non-IE browsers use the following method (method 2) to create XMLHttpRequest objects:

Var xmlhttp=new XMLHttpRequest ()

Note: Internet Explorer actually uses an object called XMLHttp instead of a XMLHttpRequest object, while Mozilla, Opera, Safari, and most non-Microsoft browsers use the latter (hereinafter referred to as the XMLHttpRequest object). IE7 is starting to use XMLHttpRequest objects as well.

So we need to create a XMLHTTPRequest object that is compatible with multiple browsers:

The first method:

Var xmlhttp=false;// creates a new variable and assigns a value of false, and uses false as a judgment condition to indicate that the XMLHTTPRequest object has not been created yet

Function CreateXMLHttp () {

Try {

Xmlhttp=new XMLHttpRequest (); / / attempts to create a XMLHttpRequest object, which is supported by all browsers except IE.

} catch (e) {

Try {

Xmlhttp=ActiveXobject ("Msxml12.XMLHTTP"); / / use a newer version of IE to create IE compatible objects (Msxml2.XMLHTTP).

} catch (e) {

Try {

Xmlhttp=ActiveXobject ("Microsoft.XMLHTTP"); / / create IE compatible objects (Microsoft.XMLHTTP) using older versions of IE.

} catch (failed) {

Xmlhttp=false;// still keeps false if it fails.

}

}

}

Return xmlhttp

}

An example of judging success:

If (! xmlhttp) {

Failed to create xmlhttp

} else {

Xmlhttp created successfully

}

The second method:

If (typeof (XMLHttpRequest) = = "undefined" & & window.ActiveXObject) {

Function XMLHttpRequest () {

Var xmlhttp_arr= ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]

Var xmlhttp

For (specify the sending address and sending method-> specify the state change handling method-> send the request, the specified processing method will be called automatically when the status changes after the request is sent.

After reading the above, do you know more about the compatibility between the creation of ajax core XMLHTTPRequest objects and browsers? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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