"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
Ralph Waldo Emerson
One of the wonderful things about the web is that it makes it so easy to look up information. This is especially true when it comes to finding an answer to a web design problem. For every problem, it seems, there are hundreds code examples on dozens of sites showing how other developers have solved the problem. If you look closely you may find there are really only a few ways to solve a certain problems and most of the code snippets rehash the same old solutions.
All modern browsers now support remote scripting. The remote scripting object is also slated for inclusion in the W3C's DOM 3 specification for the next generation of web browsers. Thus it is a firmly established technology. As long ago as IE 7, you could use the same syntax to create a remote scripting object as is used by Mozilla, Safari, Opera, Chrome, et. al., and which is also used in the DOM 3 specification.
Even if you are familiar with the remote scripting object, (also known as the XMLHTTP or just XHR object,) what is unique in this code is that it has been written as a library file and allows file reading via the remote scripting object is as simple as a server side include or the PHP include() function. This code improves error handling, removes some redundant code and adds a couple of options for the developer over previous versions and when compared to similar examples you might find on the web. It also allows multiple "includes" on a given web page, and supports displaying a image to indicate content is being loaded.
To see a live example of using remote scripting to read files into a web page please visit the page at the following link. Read File [Live Example]. The live example of the client side include function demonstrates the ability to gracefully fallback for non-remote scripting enabled browsers. The source code for the example is shown on that page but will be included here so we can look a little closer at that code's functionality.
2. Use Of The File Reading Library
In this example we use the body "onload" event to execute the "include(path/to/file.txt)" command. The include function can be called in many different ways, including from another function, any page event, a link or a button. As written in the example, the content of the remote page is not returned by the "include" function, but are received by a results handling function.
As previously mentioned, this code adds additional flexibility by allowing you to specify the results handling function, and optionally a image to indicate the remote contents are being loaded. So in the code, we would use:
Remote scripting while available in nearly every modern browser, is not available in some browsers such as screen readers for the visually impaired and in some search engine spiders.
BTW, did you know that EE itself uses this technique extensively? For instance, on a Zone Landing page, when you click the tabs (such as "Recent Activity", "Awaiting Answer", and "Articles") the script calls a function named AsyncInclude() to populate the listviews. Among other things, the result is a speed up in page load (as measured by, say, Alexa) since all that "hidden stuff" does not weigh down the initial load time.