Ajax Course

All Ajax scripts use the XMLHttpRequest object, so it's useful to know the main elements of this object.
The purpose of the XMLHTTPRequest object is to allow JavaScript to formulate HTTP requests and submit them to the server. You can have your page make such calls asynchronously in the background, allowing you to continue using the page without a browser refresh or the loading of a new page.
Before you can use the XMLHttpRequest, you have to create an instance of this object, with the syntax:

var xmlHttp = new XMLHttpRequest();
    - xmlHttp can be any variable name.
XMLHTTPRequest is supported by all modern browsers, and by the main operating systems: Windows, UNIX / Linux and Mac OS, however, its implementation differs from some browsers. Microsoft introduced the XMLHTTPRequest object, implementing it in Internet Explorer 5 (till 7) as an ActiveX object.
Because you don’t know which browser, version, or operating system your users will have, your code must adapt its behavior to ensure that the instance of the object will be created successfully.
- To create the XMLHttpRequest object for all browsers, you can use the following syntax:
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object
- The "request" variable contains an instance of the XMLHttpRequest object.

Properties of the XMLHttpRequest object

Methods of the XMLHttpRequest object


• To understand better what you can do with the XMLHttpRequest object, let's see an example.
  1) Create a txt file with a simple text. For example, "test.txt" with the text "Response from the test.txt".
  2) Add the code below into an HTML file, created in the same directory where the file "test.txt" is.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<title>Test XMLHttpRequest</title>
<script>
// sends data to an external file and return the received response
function makerequest(serverPage, tagID) {
 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object

 request.open("GET", serverPage); // define the request
 request.send(null); // sends data

 // Check request status
 // If the response is received completely, will be transfered to the HTML tag with tagID
 request.onreadystatechange = function() {
 if (request.readyState == 4) {
 document.getElementById(tagID).innerHTML = request.responseText;
 }
 }
}
</script>
</head>
<body>

<h5 style="cursor:pointer;" onclick="makerequest('test.txt', 'res')"><u><u>Click</u></u></h5>
<div id="res">Ajax course - coursesweb.net</div>

</body>
</html>
- This code is an HTML document with a JavaScript script that uses the XMLHttpRequest object. It has a <h5> tag in the BODY, used to call the JS script, and a DIV with some text in it.
- When you click on the Click word, the onclick="makerequest('test.txt', 'res')" instruction calls the makerequest() function that will send a request to the "test.txt".
- "onreadystatechange", checks the request status. When the complete response is received (readyState == 4) will be transferred by the property "responseText", with the "getElementById()" in the HTML tag that has the id of "tagID ('res').
- This example will show the following result (click on the word "click" below):
Click
Ajax course - coursesweb.net

The request may be made to files on the server that can process data received and return a response according to that data. This technology is known as AJAX. In the following lessons you can learn more about these things.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
XMLHttpRequest object

Last accessed pages

  1. Deco Tool (2719)
  2. jQuery Drag and Drop Rows between two similar Tables (12974)
  3. Creating Custom Colors (2363)
  4. Wake Up! (15294)
  5. GraidleChart Create Graphic Charts (2008)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (526)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)