Ajax browser support

Source: Internet
Author: User
Document directory
  • Example:

The main point of AJAX is the XMLHTTPRequest object.

Different browsers use different methods to create XMLHttpRequest objects.

The IE browser uses activexobject, while other browsers use JavaScript built-in objects named XMLHttpRequest.

To create this object for different browsers, we need to use a "Try and catch" statement. You can read more about try and catch statements in our JavaScript tutorial.

Let's use the JavaScript code used to create the XMLHTTPRequest object to update our "testajax.htm" file:

<HTML> <body> <SCRIPT type = "text/JavaScript"> function ajaxfunction () {VaRxmlHttp; Try {// Firefox, opera 8.0 +, SafarixmlHttp=new XMLHttpRequest();} Catch (e) {// Internet Explorer try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} Catch (e) {try {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");} Catch (e) {alert ("your browser does not support Ajax! "); Return false ;}}}</SCRIPT> <form name =" myform "> User: <input type =" text "name =" username "/> time: <input type = "text" name = "time"/> </form> </body> Example:

First, declare an XMLHTTP variable that saves the XMLHTTPRequest object.

Then use XMLHTTP = new XMLHttpRequest () to create this object. This statement is applicable to Firefox, opera, and Safari browsers. If it fails, try XMLHTTP = new activexobject ("msxml2.xmlhttp") for Internet Explorer 6.0 +. If it fails, then try XMLHTTP = new activexobject ("Microsoft. XMLHTTP ").

If these three methods do not work, the browser used by the user is too outdated. He or she will see a prompt stating that the browser does not support Ajax.

Note: The Code customized by these browsers is long and complex. However, this code can be used whenever you want to create an XMLHTTPRequest object, so you can copy and paste the code at any time you need. The above code is compatible with all mainstream browsers: Internet Explorer, opera, Firefox, and safari.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.