Home > Web Front-end > JS Tutorial > A brief discussion on browser support for Ajax

A brief discussion on browser support for Ajax

青灯夜游
Release: 2018-11-08 16:46:20
Original
2760 people have browsed it

The content of this article is to briefly talk about the browser support of Ajax, so that everyone can understand the compatibility writing method of creating XMLHttpRequest object. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First of all, we need to know that the key to Ajax is the XMLHttpRequest object, which can be used to exchange data with the server in the background. Ajax can obtain background data in the browser through it. [Recommended related video tutorials: Ajax Tutorial]

But different browsers have different methods of creating XMLHttpRequest objects. For example:

IE browser uses ActiveXObject to create, while other browsers use JavaScript built-in object named XMLHttpRequest to create.

This also leads to different browser support for Ajax. Not all browsers support Ajax. Let's take a look at the list of major browsers that support AJAX:

1, Mozilla Firefox 1.0 and above.

2. Netscape 7.1 and above.

3. Apple Safari 1.2 and above.

4. Microsoft Internet Explorer 5 and above.

5. Konqueror.

6, Opera 7.6 and above.

To create this object for different browsers, we need to write a specific piece of code. Let's take a look at the compatibility writing method of creating an XMLHttpRequest object:

Here we need to use the try...catch statement in JavaScript. If you are not familiar with the try...catch statement, you can refer to the previous The article [What is the try...catch statement in js? how to use? ]!

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script language = "javascript" type = "text/javascript">
         //浏览器支持代码
         function ajaxFunction() {
            var ajaxRequest;  // 声明一个ajaxRequest变量,用来保存XMLHttpRequest 对象

            try {
               // 兼容:Opera 8.0+, Firefox, Safari 
               ajaxRequest = new XMLHttpRequest(); //创建XMLHttpRequest 对象
            } catch (e) {

               // 兼容:Internet Explorer 浏览器
               try {
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
               } catch (e) {
                  
                  try {
                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {

                     // 出了问题时
                     alert("您的浏览器不支持Ajax!");
                     return false;
                  }
               }
            }
         }
      </script>
	</head>
	<body>
		<form name = &#39;myForm&#39;>
	       姓名: <input type = &#39;text&#39; name = &#39;username&#39; /> <br />
	       时间: <input type = &#39;text&#39; name = &#39;time&#39; />
        </form>
	</body>
</html>
Copy after login

In the above JavaScript code, we try to create the XMLHttpRequest object three times.

Our first attempt:

// 兼容:Opera 8.0+, Firefox, Safari 
ajaxRequest = new XMLHttpRequest();
Copy after login

This statement works for Opera 8.0, Firefox and Safari browsers. If that fails, we can also try twice to make the correct object on the Internet Explorer browser:

Our second and third attempts: Compatible with Internet Explorer browser

//第二次尝试:
// 兼容:Internet Explorer 6.0+
ajaxRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

//第三次尝试:
// 兼容:Internet Explorer 5.5+
ajaxRequest = new ActiveXObject(“Microsoft.XMLHTTP”);
Copy after login

If If none of these three methods work, the user's browser is out of date and he or she will see a prompt stating that the browser does not support AJAX.

Note:

The codes customized for the above browsers are very long and complex. However, this code comes in handy if you want to create an XMLHttpRequest object, so you can copy and paste this code whenever you need it. The above codes are compatible with all major browsers: Internet Explorer, Opera, Firefox, Safari, etc.

Summary: The above is all the content introduced in this article, I hope it will be helpful to everyone's study.

The above is the detailed content of A brief discussion on browser support for Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template