E B (Web site hosting) V
Thursday, January 31st, 2008E B V
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
E B V
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
CHAPTER 3 CONTENT CHUNKING PATTERN 77 In Figure 3-13, the initial page is downloaded by clicking the button. The downloaded content is JavaScript, and the initial page has no idea what the content does. When the content has been downloaded, it is executed. The HTML framework page has coded the referencing of the variable dynamicFilter and the calling of the method MakeCall. The MakeCall method does not exist when the HTML framework page is executed, and is available when the downloaded content is executed. The downloaded content that is executed downloads yet another piece of content that is injected into the HTML page. The result is the loading of an image where the text Nothing was. The role of the HTML framework page has changed into a bootstrap page that loads the other code chunks. The other code chunks are purely dynamic and contain references and code that the HTML framework page does not know about. The advantage of this implementa tion is that the document can be loaded incrementally by using pieces of dynamic code that are defined when they are loaded. The Content Chunking pattern defines the loading of content dynamically. But the additional use of JavaScript makes it possible to dynamically define the logic that is used by the HTML framework page. Pattern Highlights The following points are the important highlights of the Content Chunking pattern: An HTML page is the sum of an HTML framework page and content chunks. E B V N The HTML framework page is responsible for organizing, referencing, and requesting the appropriate chunks. It should act as a mediator for the individual chunks. The HTML framework page delegates the processing of the chunks to another piece of code. The content chunks are uniquely identified by a URL. Content chunks that are distinct do not use the same URLs. Content chunks are used to implement functionality that is determined by the user. Content chunks should be one of three types: XML (preferred), HTML (preferred XHTML), or JavaScript. There are other formats, but they are not covered in this book and their use should be carefully considered.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
76 CHAPTER 3 CONTENT CHUNKING PATTERN The example JavaScript source code is formatted using object initializers. An object initializer is the persisted form of a JavaScript object. You should not equate an object initializer with a JavaScript class definition; they are two entirely separate things. When an object initializer is processed, an object instance is created, and the identifier of the object instance is the variable declaration. In the example, the variable dynamicFiller is the resulting object instance. The variable dynamicFiller has two properties (generatedAsyncand reference) and two methods (complete and makeCall). The property generatedAsync is an instantiated Asynchronous type and is used to make an asynchronous call to the server. The property reference is the HTML element that will be manipulated by the method complete. The method makeCall is used to make an XMLHttpRequest, and the parameter destination is assigned to the property reference. Putting all the pieces together, the HTML framework code contains general code that references an incomplete variable. To make a variable complete, the JavaScript content is downloaded and executed. The complete variable contains code to download content that is injected into the framework page. Figure 3-13 illustrates the execution sequence of events. Figure 3-13. Sequence of events when downloading and executing JavaScript E B V N
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
CHAPTER 3 CONTENT CHUNKING PATTERN 75 Consider the following example HTML page:
| Nothing |
When the user clicks the Get Script button, an XMLHttpRequest request is made that retrieves the document /chap03/chunkjs01.html. The document contains a JavaScript chunk that is executed by using the eval function. The following chunk is downloaded: window.alert(”oooowweee, called dynamically”); The example chunk is not very sophisticated and pops up a dialog box. What would concern many people with arbitrarily executing JavaScript code is that arbitrary JavaScript code is being E B V N executed. An administrator and user might be concerned with the security ramifications because viruses or Trojans could be created. However, that is not possible because JavaScript executes within a sandbox and the same origin policy applies. Granted, if a developer bypasses the same origin policy, security issues could arise. When receiving JavaScript to be executed, a simple and straightforward implementation is to dynamically create a JavaScript chunk that executes some methods. The JavaScript chunks make it appear thatthewebbrowser is doing something.For example,the JavaScript chunk downloaded in the previous example could be used to assign the spanor td tag as illustrated here: document.getElementById(”mycell”).innerHTML = “hello”; The generated script is hard-coded in that it expects certain elements to be available in the destination HTML page. Generating a JavaScript That Manipulates the DOM Earlier you saw the image generation solution in which an image was broken and then made complete by downloading a valid link. It is also possible to download an image by modifying the Dynamic HTML object model. You modify the object model by using a JavaScript chunk to insert the img tag. The following is an example image JavaScript chunk that creates a new img tag and chunks it into the HTML document: var img = new Image() img.src = “/static/patches01.jpg” document.getElementById(”insertplace”).appendChild(img)
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
72 CHAPTER 3 CONTENT CHUNKING PATTERN It seems a bit odd to download and assign links that are then processed by the web browser. This indirect approach is done not to illustrate how complicated a web application can be made. The indirect technique is necessary because directly downloading binary data is not possible. But all is not lost, because of the way that the browser caches images. If an image is referenced and downloaded, the image stays in the browser s cache. If the image is referenced a second time, the image is retrieved from the cache. Of course this happens only if the HTTP server implements caching. There is a downside: If a request is made for a URL that references an image, two HTTP requests are required: one to download the content that contains the URL of the image, and the image itself. If both requests are using HTTP 1.1, which most likely is the case, the requests will be inlined using a single connection. Another variation of the illustrated strategy is to download not a URL but the entire HTML to create an image. The strategy does not save a request connection, but provides a self-contained solution that involves no additional scripting. The following HTML code snippet illustrates how the entire img HTML tag is downloaded:
When injecting both the imgtag and its appropriate srcattribute, the browser will dynam ically load the image as illustrated in the previous example. The advantage of injecting the HTML is that the server side could inject multiple images or other types of HTML. Additionally, by injecting the entire imgtag, there is no preliminary stage where a broken image is generated. However, either approach is acceptable, and which is used depends on the nature of the appli cation. When injecting HTML, there might be a flicker as the HTML page resizes. When you E B V assign the srcproperty, there is no flicker, but an empty image needs to be defined or the image element needs to be hidden. JavaScript Chunking Another form of chunking is the sending of JavaScript. Sending JavaScript can be very effective because you don t need to parse the data but only execute the JavaScript. From a client script point of view it is very easy to implement. For reference purposes, do not consider downloading JavaScript faster than manually parsing and processing XML data and then converting the data into JavaScript instructions. JavaScript that is downloaded needs to be parsed and validated before being executed. The advantage of using the JavaScript approach is simplicity and effectiveness. It is simpler to execute a piece of JavaScript and then reference the properties and functions exposed by the resulting execution. Executing JavaScript Consider the following HTML code that will execute some arbitrary JavaScript:
CHAPTER 3 CONTENT CHUNKING PATTERN Figure 3-10. Initial HTML page generated without an image E B V N Figure 3-11. The HTML page after the image has been downloaded
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.
70 CHAPTER 3 CONTENT CHUNKING PATTERN