Home
Home Page
Than differ id and class
Creation WAP of a page
Passions around AJAX are heated
Answers to often asked questions on XHTML and HTML
The sanction of the screen and marking of pages
Two styles ajax'?
Business Macromedia lives
XHTML+CSS. Advantages are obvious
Microformats
The semantic network based on microformats
Fonts and CSS
CSS from And up to I
Properties of the text
Illumination of the link
Change of a background in the table
Fixation of fonts on page with the help of the table of styles
Krossbrauzernoe alignment on the center
Polling with help AJAX
HTML
Links
 

Two styles ajax'?

Ajax starts on the sly materet` as technology, and in him various patterns, approaches, schools (and with them there are also obligatory sacred wars) are already made out. In particular at several people already (for example note Frehnka Sommersa which and spodvigla to a spelling of this) I read me that essentially different two approaches to a spelling of ajax-applications were issued:


* Creation ready HTML on the server with automatic his{its} introduction in the necessary place of page

* Transfer on page only the structured data and change on them HTML'? a script on the client


In spite of the fact that Frehnk writes, that these two approaches are incompatible, I have found out, that itself just them I mix, and it seems to me that successfully:-).



More in detail about approaches


Transfer HTML


The approach with transfer HTML'? ideally approaches for pages which at interaction with the server vary insignificantly. For example a certain form which the ambassador sabmita on the server again appears on the screen, and together with her or is drawn a picture that very well, or the list of mistakes is drawn. In this case ajax allows to reduce quantity{amount} of the data transmitted on a network, accepting with the server not the form with indication, but only indication.


Exclusive simplicity of idea from which directly follows concerns to doubtless pluss of this approach, that on the client party{side} it is possible to automate practically all:


* Substitution sabmita forms javascript'ovym obrabotchikom

* Formation by it skriptovym obrabotchikom the data of the form in a kind, suitable for sabmita on (name1=value1*name2=value2 server

* Reception of the answer and his{its} introduction in page


Actually everything, that it is necessary to make to the programmer it:


* To stick with a finger into by what button he wants " to make ajax ", and in what block on page then to write down result

* On the server to explain frejmvorku, which part of the answer should be cut out for ajax'nykh searches


I would attribute{relate} that he works for "vysokodinamicheskikh" pages where from one action different parts vary essentially worse to minuses of the approach. For example long tred comments where on sabmitu forms with the text from the server there should come the new comment and, say, to be updated the line " N comments " which is in absolutely other place of page. Here, if to follow automatic model, it is necessary to pass that its{her} part which includes all necessary pieces even if between them there are data which were not assumed to vary to page. In practice it can lead to to that on a network the rather big volumes of the data comparable with the size actually of page chase, and all sense ajax'? degenerates in absence of effect of animation of an icon of a browser in a right top corner.


And my underlined "can", unfortunately, it appears the truth more often, than it would be desirable, because such completely the automatic approach involves many developers who have no time and-or desire to understand with skriptovaniem, and rely that frejmvork will help them to make it small blood. Therefore they cannot neither notice in time, nor correctly correct problems of lower level.


Transfer of the structural data


Transfer of the structured data was essence ajax'? still when he was called "XmlHttpRequest". That is he has been thought up to address on the server and to receive the description of any part of a status of the system expressed in XML - language which is intended for convenient machining. At once I shall say, that in spite of the fact that so it has been conceived, light on XML-answers was not left by a wedge, and the structure can be passed and in another way, about what hardly later.


Plus of such approach - in the maximal flexibility. It is possible to think up absolutely any format and a data set which is requested of the server in which there will be in accuracy that information which does not suffice on the client, and anything superfluous. On the client with help Javascript'? it is possible to change page completely and somehow.


To follow the same example with comments: on the server the comment is sent, and in the answer comes back for example such:



<comment status = "ok" quantity = "12"/>


That means, that the comment has passed validaciju, and now them on page 12. The everything else can be made a script: to add the new block of the comment, to copy there the text formochki, fomochku to remove, update quantity{amount} in new figure.


The most obvious minus in transfer of structure is a much more volume and complexity of programming. And there is she of several things.


The first complexity is use XML'?. In spite of the fact that a format precise and remarkable, in Javascript'? with it{him} it is possible to work only with the help of methods DOM. And it uneasily to write and very difficultly to read: it is too much text for manufacture of simple things. Fortunately, this concrete lack recently was solved with the help of format JSON: the data come back from the server not as XML, and as piece Javascript'ovogo of a code creating object of the structure necessary to you:



response = {

status: 'ok',

quantity: 12

}


He there and then in a script is carried out eval'om and you receive object response at which is response.status and response.quantity. very conveniently and without everyones



getElementsByTagName ('response') [0].


Except for ubiranija complexities of analysis of the XML-answer, there is a second complexity: construction HTML completely manually to DOM-methods, that, besides, difficultly and uneasily.


But the most bad, that this most in system appear completely different two places in which the kind of page is set: a HTML-pattern and Javascript'ovyj a code. So changes should be duplicated accurately both there, and there. And more it means, that the person who is able to write and HTML, and heavy Javascript. " Maintenance hell " now is necessary for spelling HTML'?, differently for you.


In general, when I started to be played with XmlHttpRequest'om, such approach seem to me the most natural: time a piece is able parsit` XML, it means and there is a correct way of its{her} use. Result of those exercises became enough prikol`nyj the client ajax-interface to a chat with set of everyones fenechek. Then, in 2003, he was terribly advanced technologically, but to untwist it{him} in something known we and were not honoured. I even was surprised now, that he till now works. Let now here the reference for a history lays:-)



Association of the best features


That incompatibility about which speaks Frehnk Sommers, is present only from the point of view of the programmer who chooses transfer HTML, and does not want to be immersed in skriptovanie in general. However if the programmer not against it essentially transfer of structure can be united with transfer HTML'?. A principle, actually, very simple:


* The answer from the server is passed as JSON-object

* The things which have been not connected to generation HTML (for example podvinut` something somewhere to hide / open and t.d), are carried out by a script, using for this purpose the data from object

* If it is necessary to change any not trivial pieces HTML they are passed by lines in the same object and are inserted where it is necessary

* Changeable parts HTML on the server are made out as small included shablonchiki, and used and at initial formation of page, and in ajax-answers


In other words, all " great idea " consists that is necessary to refuse dogma " to use only XML'om " and to use that what it is convenient.


The only thing, that yet is not pleasant to me in this approach, it that on the server party{side} the volume of job a little increases. If without ajax'? the code serving POST in the form, gives only two answers - or the same form with mistakes, or a redirect when very well - that with ajax'om to each answer increases still a variant with delivery of JSON-object.s not especially difficultly, but looks too heaped up. Though in due course I hope to think up or peep any automation to this process.