Blogroll

photoshop cs6 html 5 css php seo backlinks

adsense

Monday, 24 February 2014

AJAX: Shortcut to AJAX

AJAX Tutorial: In this post, We try to use Ajax in our simple application. First time, We will build simple ajax library. Then, we write page to load the ajax library. This is very basic application. You can still extend it for more complex web.
Ok, for this practice, we need 3 file (I create within www/test/ajax directory):
  1. ajax.js -> as ajax library
  2. test.php -> as main page
  3. home.html -> will be loaded as content
First, write below line codes within ajax.js
01function callAJAX(url, pageElement, callMessage) {
02      document.getElementById(pageElement).innerHTML = callMessage;
03      try {
04        req = new XMLHttpRequest(); /* e.g. Firefox */
05      } catch(e) {
06          try {
07          req = new ActiveXObject("Msxml2.XMLHTTP");
08   /* some versions IE */
09          } catch (e) {
10              try {
11              req = new ActiveXObject("Microsoft.XMLHTTP");
12  /* some versions IE */
13              } catch (E) {
14                 req = false;
15              }
16          }
17      }
18 
19      req.onreadystatechange = function() {responseAJAX(pageElement);};
20    req.open("GET",url,true);
21      req.send(null);
22 
23  }
24 
25function responseAJAX(pageElement) {
26    var output = '';
27    if(req.readyState == 4) {
28         if(req.status == 200) {
29              output = req.responseText;
30              document.getElementById(pageElement).innerHTML = output;
31            }
32       }
33   }
then, we create test.php, enter following codes:
01<html>
02<head>
03    <SCRIPT language="JavaScript" SRC="ajax.js"></SCRIPT>
04</head>
05<body onload="callAJAX('home.html','displaydiv')">
06 
07<table>
08<tr>
09<td id="displaydiv"></td>
10</tr>
11</table>
12 
13</body>
14</html>
last, we create home.html, enter following line code:
1front page test
Now, point your browser to http://localhost/test/ajax/test.php, you will get like this:

0 comments:

Post a Comment