Due to numerous requests from beginners (and not only) AJAX developers, I decided to allocate some of my time to describe some of the tricks and techniques that I use when building complex, dynamic web applications. The article is a mixture of theoretical calculations and practical examples.
The greatest difficulties in building websites using AJAX arise when implementing the
AHAH (Asynchronous HTML and HTTP) mechanism. Especially on-the-fly execution of loadable javascript. There is also a bunch of additional “hemorrhoids” when developing AJAX sites and web applications. You can read a detailed
description of some existing problems . Due to these difficulties, many developers refuse to implement AJAX more intensively.
However, few people know that these problems can be solved.
Moreover, there are already a number of solutions that make it easy to bypass problem and bottlenecks when developing complex AJAX web applications, as well as dynamically loading HTML pages with embedded scripts.
')
One of these solutions is Fullajax, a technology for building websites and web applications using AJAX. This is a combination of algorithms, approaches, rules and methods for maximizing the full power of AJAX & AHAH. The technology aims to reduce the complexity and increase the focus of the AJAX application.
Fullajax is expressed in a separate independent library. This is not an analogue of jQuery, Mootools, ExtJS, Prototype, YASS. However, sharing with modern javascript libraries does not contradict and does not cause conflicts, and on the contrary complements each other. The library is intended solely for communication. The whole emphasis is placed on convenience, flexibility and at the same time ease of use of methods and functions for the exchange of data between the client and the server. In the previously listed libraries there is DOM navigation and manipulation, various visual effects, sets of visual components. Fullajax doesn't have it all. But there are other possibilities.
List of main features:
- implementation of a simple communication interface
- Fly AJAX (conversion to AJAX "on the fly"), AJAX filter - links and forms according to the specified criteria are automatically converted to AJAX
- the possibility of indexing by search engines like on a regular website
- parsing and execution of scripts, links and styles in content dynamically loaded web pages
- script loading accelerator (accelerator) - parallel loading with sequential use
- several levels of caching HTML pages, scripts and styles
- the ability to disable the reinitialization of script functions (re-execution) - improves performance, saves computational resources and RAM
- support for direct links to “AJAX generated” web pages
- Implementing AJAX navigation history (“Back” and “Forward” browser buttons)
- full document.write and <frameset> processing; in each stream of dynamically loaded pages
- support for onload and onunload of the <body> tag and window.onload
- TITLE changes according to loadable content
- multithreaded emulation, separate history for each stream
- correct meter operation
- banner updates with each update
- AJAX upload files to the server (upload) without rebooting
- implementation of a simple event model
- the ability to use local storage flash storage
- Two models of HTML pages request - “one-to-one” and “one-to-many” (one request - the answer to many blocks)
- local file system support
- the ability to share standard anchors and AJAX links
Of course, for one article to talk about everything, especially with examples and in details will not work, as it is expensive and time and the number of letters.
This article will discuss the most important functionality of the library. It is planned to write a series of articles. Let's start with the simplest.
1. access to the item by id
<div id= "my-div" ></div>
<script>
id( 'my-div' ).innerHTML = 'test1' ;
//
SRAX.get( 'my-div' ).innerHTML = 'test2' ;
</script>
2. SRAX.onReady - DOM Readiness
The function that is called immediately when the DOM is ready. Such a function exists in all modern zhabaskriptovy libraries.
SRAX.onReady( function (){alert(1)})
function mytest(){
document .body.appendChild( document .cereateElement( 'div' )).innerHTML = 'New Div' ;
}
SRAX.onReady(mytest)
Now directly on the communication functions.
To work with AJAX requests in the library, two functions dax and hax are implemented.
The basic rule is:
- for querying textual data, xml, json, and the like, you must use dax ( D ata A ja X )
- for html request, you must use hax ( H tml A ja X ) - this method is AHAH support (Asynchronous HTML and HTTP)
It's simple, if you load the HTML code that needs to be inserted into the page, use hax, otherwise dax.
3. An example of using dax
dax({url: 'myurl' , id: 'save-data' , form: 'myform' , method: 'post' , cb: function (resp, cbo){
alert(resp.text || resp.xml)
}})
data of all fields (input, textarea, select, ...) from a form where id = 'myform' will be automatically collected and sent to url = myurl by the post method
id - defines the internal identifier of the stream, is designed to separate the request streams to the server, two threads (requests) with the same id cannot exist at the same time, the next request cancels the previous one. Using id also allows you to reuse objects created in memory.
List of dax operation options
- url / src - request URL
- id - internal stream id
- method - the method of requesting data post or get (default)
- form - form id, form itself, element id or element itself from which parameters must be collected
- params - the string of parameters that must be included in the request (name = val1 & name = val2)
- callback / cb - callback function
- callbackOps / cbo - options that are passed to the callback function
- destroy - flag of auto deleting process from memory after the request is true or false (by default)
- anticache / nocache - anti-caching flag true or false (default)
- async - asynchronous query execution flag true (default) or false
- xml - XML, emulation of the response, if this parameter is present, the request for data from the server is not performed
- text - text, response emulation, if this parameter is present, the server does not request data from the server
- user - username, for connecting requiring the user name
- pswd - password, for connecting requiring a password
- headers is an array of headers of {key: value} objects that must be passed to the server. example -> headers: [{Etag: '123'}, {'Accept-Encoding': 'gzip, deflate'}]
- loader - loader indicator, if not specified - default loader is used
- storage - the flag of using local storage is true (by default) or false - it is valid only when SRAX.Storage is connected
- etag - the flag of using Etag to identify the novelty of data in the local storage true (default) or false - is valid only when SRAX.Storage is connected
4. hax usage example
~ 70% of all library code is an implementation of hax. This function performs all the complex operations of processing different variants of HTML code. You can say hax is a full-fledged core of the analysis and processing of HTML documents. It automatically selects and applies styles and jabscripts, regulates their loading sequence, manual loading method (via dax) or automatic (returning the loading process to the browser), implementing AJAX navigation history, document.write processing, window.onload, automatic title change, support direct AJAX links, more.
Consider the internal hax operation algorithm. When you receive a html response, the document is analyzed and parsing of styles and scripts. If styles and external scripts are made, they are automatically recursively reloaded. Recursive means if inside the reloading scripts, styles are still external links, the process is repeated. If scripts, styles from another domain, the download process is then given to the browser and the subsequent analysis of such scripts, styles are not performed.
To implement a clear and stable work when processing complex HTML pages, an algorithm was developed to control the loadability and sequence of execution of scripts, styles and links, which removes these responsibilities from browsers. Due to this, a good, stable, and most importantly equally predictable work of AHAH in the main web browsers is implemented.
One of the “stones in sandalwood” is the joint use of AJAX and document.write. When document.write appears in scripts, site operation using AJAX (AHAH) is impossible by default. Since document.write can only be used until the end of the main page output stream. Otherwise, the page content is simply erased. In Fullajax, the document.write function is redefined, so that what was intended by the programmer is executed without error. The algorithm for emulating this function was one of the most difficult moments when developing a library.
Also one of the most complicated functionaries of the library is the implementation of cross-browser AJAX navigation history. In the end, in Fullajax, working with AJAX history comes down to defining one parameter on / off.
Examples of working with hax:
hax({url: 'index.html' , id: 'my-div' })
The element with id = 'my-div' will be loaded with the page 'index.html' using AJAX history.
hax({url: 'index.html' , id: 'my-div' , nohistory:1, onload: function (){
alert( 'Hey Bingo!!' )
}})
In the element with id = 'my-div' the page 'index.html' will be loaded without using AJAX history, after loading an alert will fly out.
List of hax options
- url / src - request URL
- id - the id of the parent element
- method - the method of requesting data post or get (default)
- form - form id, form itself, element id or element itself from which parameters must be collected
- params - the string of parameters that must be included in the request (name = val1 & name = val2)
- callback / cb - callback function
- callbackOps / cbo - options that are passed to the callback function
- nohistory (noHistory) - flag for using AJAX navigation history, default is false - i.e. history included
- rc - use (true) or not use (false - by default) correction of relative reference paths
- overwrite - true or false flag (by default) of overwriting onclick and onsubmit events during Fly AJAX (by default, functions on onclick and onsubmit events are not overwritten, but added)
- destroy - flag of auto deleting process from memory after the end of the request
- html - HTML text, response emulation, if this parameter is present, the request for data from the server is not performed
- anticache / nocache - anti-caching flag true or false (default)
- startpage - the sign of the first page of the story is true or false (by default)
- async - asynchronous query execution flag true (default) or false
- historycache - history cache use flag true or false (USE_HISTORY_CACHE is used by default)
- seal - “isolation” flag true or false (default) - used to resolve css style conflicts
- user - username, for connecting requiring the user name
- pswd - password, for connecting requiring a password
- headers is an array of headers of {key: value} objects that must be passed to the server. example -> headers: [{Etag: '123'}, {'Accept-Encoding': 'gzip, deflate'}]
- add - flag true or false (default), defines the principle of adding loadable content to the parent element: overwriting (false) or adding (true)
- onload is a function that is called after the content is fully loaded into the item.
- loader - loader indicator, if not specified - default loader is used
- notitle - true or false flag (default), disables auto-change of title
- onerror - a function that is called in case of a content download error.
- storage - the flag of using local storage is true (by default) or false - it is valid only when SRAX.Storage is connected
- etag - the flag of using Etag to identify the novelty of data in the local storage true (default) or false - is valid only when SRAX.Storage is connected
Continuation
If God will give time and energy, in the next article (if the topic is interesting to readers), it is planned to highlight a number of issues such as:
- Filter (Fly AJAX) - principles and examples of automatic "wrapping" of simple links in AJAX links
- directLink - support direct AJAX links
- linkEqual - converting AJAX links into a human-friendly view
- addScript - dynamically add scripts (even with document.write)
- script loading optimization, Fullajax algorithms with script processing
- addCss, addStyle - dynamically add styles
- Storage - Flash-based local storage.
- Anchor - the ability to share standard anchors and AJAX links (unique development)
- model2Marker - one-to-many request (one request, auto fill in several different elements)
- ax family: attributes
- Effect - a function for applying the effects of updating HTML pages
- Include - implementation of the <include src = "index.html"> </ include> tag
- Uploader - Quasi-AJAX File Uploader
- addEventsListener - add event model objects to objects
- Default - default operation parameters
- advantages and disadvantages of AHAH sites over normal
All the best, we will write off ...
UPD1: Some Case Studies
Dream Travel - Travel Company
Tooglecms - the truth is still damp, only the first beta.
Datamash.us - platform widget
Forum where you can ask technical questions