A. Brief introduction
AJAX is a technique that can update parts of a Web page without reloading the entire page.
AJAX is a technique for creating fast, Dynamic Web pages. AJAX enables Web pages to be updated asynchronously by exchanging small amounts of data in the background with the server. This means that you can update a part of a webpage without reloading the entire page. Traditional Web pages (without AJAX) if you need to update the content, you must reload the entire page surface.
In 2005, Google made AJAX popular through its Google Suggest. Google Suggest uses AJAX to create a highly dynamic Web interface: When you enter keywords in Google's search box, JavaScript sends these characters to the server, and the server returns a list of search suggestions.
Before you learn, you need to have a basic understanding of the following knowledge:
- Html/xhtml
- Css
- Javascript/dom
Two. XMLHttpRequest
XMLHttpRequest is the basis of AJAX.
All modern browsers support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).
XMLHttpRequest is used to exchange data with the server in the background. This means that you can update a part of a webpage without reloading the entire page.
Create a XMLHttpRequest object
All modern browsers (ie7+, Firefox, Chrome, Safari, and Opera) have built-in XMLHttpRequest objects.
syntax for creating XMLHttpRequest objects:
Variable=new XMLHttpRequest ();
older versions of Internet Explorer (IE5 and IE6) use ActiveX objects:
Variable=new ActiveXObject ("Microsoft.XMLHTTP");
To handle all modern browsers, including IE5 and IE6, check whether the browser supports XMLHttpRequest objects. If supported, the XMLHttpRequest object is created. If not supported, create the ActiveXObject:
var xmlhttp;if (window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest (); } else {//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP"); }
Three. Ajax instances
AJAX instance Explanation
Change content with Ajax
An AJAX application consists of a div and a button.
The div section is used to display information from the server. When the button is clicked, it is responsible for invoking the function named Loadxmldoc ():
Onclick= "Loadxmldoc ()" >change content</button></body>
Next, add a <script> tag to the head section of the page. This tag contains the Loadxmldoc () function:
Four. Pros and cons
Advantage:
1. Enhance the user experience
2. Increase productivity
3. Reduce bandwidth usage and increase access speed
4. Very good compatibility
5. Support Asynchronous processing
Disadvantages:
1. Non-compatibility between browsers
2. Unsafe
3. Increased load on the Web server
See:
Http://www.w3school.com.cn/ajax/ajax_intro.asp
Http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_create.asp
Https://www.onmpw.com/tm/xwzj/web_80.html
Java EE Week Fifth