Even when the programming logic above described is quite simple, the example will be helpful for understanding how to handle http requests without the need to look at AJAX. Considering this possibility, I’ll define the first function of the script, “sendRequest()”, which accepts the name of the file to be requested and fetches the file by using only a few DOM methods. Its definition is as follows:
As I explained before, the above function only uses standard DOM methods to request a given file. It takes the file name as an argument and builds a <script> node on the fly, by specifying a couple of attributes for the element just created. Just take a look at the line below:
Once the <script> node has been appended to the document tree, this statement is really instructing the browser to request the URL specified as the value for the “src” attribute. As a result, if I’d pass in as an argument a “news.xml” file, like this:
the specified file would be requested, and incidentally all the code –- server and client statements -- would also be executed. On the plus side, the above method eliminates the limitations imposed by AJAX, since it allows you to silently request files from any domain, while AJAX will only fetch documents from the originating server (except when the script is run as localhost).