Bases of technology AJaX

Creation of sites in increasing frequency means use of technology AJaX (asynchronous javaskript and XML).


Using the specified technology, it is possible to make easily of your site the interactive center, having transformed it  in the personal manager - napominalku, the high-grade post client or the application for conducting accounts department.


The essence of technology AJaX consists that the user does not have necessity to overload all page completely as it was earlier as after full loading page connection with the server actually remains not broken off. It is possible to program a site so, that at a clique on separate links, his  part, for example, the list of new letters will be reloaded only.


It is necessary to note also, that use of technology AJaX will allow to save the traffic of your users essentially.


Techniques of creation of sites with use of technology AJaX


There are some methods of creation of sites with use of technology AJaX:


- Use of object XmlHttpRequest;

- Use ActiveX components from Microsoft;

- Use of the invisible floating frame;


It is recommended to use the invisible floating frame as first two technologies do not give the full control over objective model DOM. Besides as you understand, use ActiveX components from Microsoft not only that it is forbidden on many workstations so also works only in Internet Explorer.


So, we shall start and we shall try to create simple realization AJaX by means of the invisible frame. Let's create JavaScript-function which will transfer contents of the floating frame to the layer necessary to you.



1 var AJaXSRC;

2 var AJaXTarget;

4 function initAJaX (obj) {

5 if (! AJaXTarget) AJaXTarget ='AJaXContent ';

6 if (! document.getElementById (AJaXTarget) ||! document.getElementById (' AJaXFrameScripts')) return false;

7 var parentScripts=obj.document.scripts;

8 if (! parentScripts) parentScripts=obj.document.getElementsByTagName ('script');

9 if (document.getElementById (AJaXTarget)) {

10 var Output = document.getElementById (AJaXTarget);

11 if (obj.document.body) document.getElementById (AJaXTarget) .innerHTML = obj.document.body.innerHTML;

12}

13 if (parentScripts)

14 for (i=0; i <parentScripts.length; i ++)

15 document.getElementById ("AJaXFrameScripts") .text + = parentScripts [i] .text;

16 return true;

17}


Let's disassemble this piece of a JavaScript-code. In variable AJaXSRC we shall store{keep} last requested URL. She is required to us hardly later. Permennaja AJaXTarget comprises id an element in which we shall build the received HTML-code.


On 5-th page it is checked, whether variable AJaXTarget which is responsible for is established into what object to insert the received HTML-code.

In case the variable is not set, to her the default value "AJaXContent" will be appropriated{given}.


On 6-th line it is checked, whether there are in objective model DOM elements with identifiers AJaXTarget and "AJaXFrameScripts".


On 7-8-® lines in a file parentScripts all elements of a kind <script> from an affiliated window enter the name. As a rule, this opportunity is not used, nevertheless, if it is necessary for you to load the document which comprises besides HTML a code also JavaScript, this code will help you.


On 9-12-® lines HTML the code contained in an element with identifier AJaXTarget (to default, "AJaXContent"), is replaced on HTML a code loaded in the floating frame.


On 13-15-® lines all scripts move.


That all worked, it is necessary to create a little HTML elements:



1 <script id = "AJaXFrameScripts">

2 <iframe name = "AJaXIOFrame" onload = " initAJaX (this) " style = "display:none"> </iframe>

3 <div id = "AJaXContent"> </div>


Now it is possible to load safely into a layer with identifier " AJaXContent " any page. Everything, that for this purpose it is necessary to make - to create a hyperlink, having specified her target = "AJaXIOFrame". If necessary, it is possible to add also in a hyperlink attribut onclick = " AJaXTarget ='OtherAJaXDiv ' " if want that the page loaded in the invisible frame was transferred not to a layer "AJaXContent", and in object with identifier " OtherAJaXContent ".


That's all, anything complex , as you can see. If to understand, as follows, it becomes clear, that use of the invisible frame and high-grade access to objective model DOM inside this frame allows to get up simply to miracles.