Datasheet
Summary of content (30 pages)
This is an incorrectly nested H1 tag
PAGE 11
Chapter 1: Introducing Ajax document.form1.button.value = “Click Me”; Or, you can use methods that can access the specific elements or subsets of elements on the page, such as the document.getElementById method, which will return a specific instance of an element that matches the criteria: var myTextBox = document.getElementById(“myTextbox”); You can then assign values to the variable you have created to alter the values. To make the text box invisible, you could call the following: myTextBox.style.
PAGE 12
Chapter 1: Introducing Ajax A fair amount of Ajax code will deal with handling cross-browser code and handling errors if and when they arise, unless you can guarantee that your target audience will only ever use one browser (such as on a local intranet). This is an unfortunate set of circumstances that even new versions of IE and Firefox are not able to rectify. Later chapters address both of these dilemmas.
PAGE 13
Chapter 1: Introducing Ajax
Hotel Name | Number of Rooms | Location |
Hotel Shakespeare | 50 | Birmingham |
Hotel WashingtonPAGE 14Chapter 1: Introducing Ajax You could then insert the XHTML document fragment into the HTML page dynamically to update the page. XSLT is another standard maintained by W3C. Both IE and Mozilla have XSLT processors; however, they don’t always treat their XSLT documents in the same way. XSLT uses another language, XPath, to query the XML document when applying its transformations. PAGE 15Chapter 1: Introducing Ajax In Mozilla Firefox, IE 7, and other browsers, it is created as follows: var xHRObject = new XMLHttpRequest(); In turn, this means that you have to do a little bit of browser feature detection before you can determine in which way the object needs to be created. Because ActiveX controls and objects are unique to IE, you can test for them by attempting to call the ActiveXObject method of the window object. PAGE 16Chapter 1: Introducing Ajax As with JavaScript, this book does not teach you how to use PHP and ASP.NET. It’s expected that you will know either one or the other. Server-side technologies are examined more closely in Chapter 3, but the current discussion attempts to avoid going into them in too much detail because they provide the glue for Ajax, rather than the backbone. PAGE 17Chapter 1: Introducing Ajax As mentioned, this model works for browsing web pages, but the development of more and more complex web applications means that this model is now breaking down. The first area in which it breaks down is that of performance. The “enter, send, and wait” approach means there is wasted time. Second, whenever you refresh a page, you are sending a new request back to the server. PAGE 18Chapter 1: Introducing Ajax Invisible Data Retrieval The longer you look at a web page, the greater its chance to go out of date. With an Ajax application, even though on the surface the web page might not be told to do anything, it could be updating itself behind the scenes. Constant Updating Because you’re not waiting for a page refresh every time, and because Ajax can retrieve data under the covers, the web application can be constantly updated. PAGE 19Chapter 1: Introducing Ajax A considerable number of articles explain why you shouldn’t use Ajax techniques in certain situations, such as those found at the following URLS: ❑ Ajax sucks — http://www.usabilityviews.com/ajaxsucks.html (a spoof article on Jakob Nielsen’s why frames suck) ❑ Ajax promise or hype — http://www.quirksmode.org/blog/archives/2005/03/ ajax_promise_or.html ❑ Ajax is not cool — http://www.lastcraft.com/blog/index. PAGE 20Chapter 1: Introducing Ajax You should be getting a feel by now that each of these reasons is qualified — the use of Ajax can hinder usability, not aid it; however, if development is considered, if you take steps to address the problems with Back buttons and bookmarks, then there’s no reason why Ajax techniques shouldn’t be used. It does raise a question, though. PAGE 21Chapter 1: Introducing Ajax Creating an Ajax application is about the design and thinking about how the user interface and experience will be improved by your application. We’re going to create a quick example that shows how you can use the XMLHttpRequest object and XML files to create a dynamic menu system for a business scenario. The business scenario is this. You have a travel company whose web site displays information about hotels and suites. PAGE 22Chapter 1: Introducing Ajax PAGE 23Chapter 1: Introducing Ajax var fragment = xsltProcessor.transformToFragment(xmlDoc, document); document.getElementById(“menuhere”).innerHTML = “”; document.getElementById(“menuhere”).appendChild(fragment); } if (spanb != null) { spanb.innerHTML = transform; } //Clear the object and call the getDocument function in 10 seconds xHRObject.abort(); setTimeout(“getDocument()”, 10000); } } function getDocument() { //Reset the function xHRObject. PAGE 24Chapter 1: Introducing Ajax Hotel London | | |
Hotel Rome | ||
PAGE 25Chapter 1: Introducing Ajax 5. Next is the CSS, which controls the switching on and off of the dynamic submenu and the presentation of the menu blocks, such as the colors and font size. Save this as SuiteListStyles .css in the Chapter1 folder. td.menublock { background-color:#eeeeff; width:120px;height:25px;border-style:ridge; borderwidth:thin; border-color:gray; font-weight: bold; text-align:center; font-family: Arial; color:gray; } div. PAGE 26Chapter 1: Introducing Ajax Just as with server-side technologies, the XMLHttpRequest object requires the server to work correctly. If it works correctly, you will see a page with five menu options on the left-hand side. Four of these options are inactive. The active option is Hotel Rome, and if you hover the mouse over it, you will see a blue menu appear (Figure 1-8). Figure 1-8: Hovering over the active option of Hotel Rome. 8. Now, this is the neat bit. PAGE 27Chapter 1: Introducing Ajax 10. The top entry has disappeared. How It Works As mentioned previously, this discussion does not go into detail about this example because the techniques will be covered in later chapters. Instead, let’s look at what the application is doing and how the different parts fit together. We’ve created a simple menu system in an XSL template. This consists of a table with five rows. The XSL template takes its data from the XML document SuiteList.xml. PAGE 28Chapter 1: Introducing Ajax // Create the XMLHttpRequest var xHRObject = false; if (window.XMLHttpRequest) { xHRObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { xHRObject = new ActiveXObject(“Microsoft.XMLHTTP”); } The other three steps are contained in the getDocument() function, which is loaded on startup, via the body element’s onload event handler: //Reset the function xHRObject. PAGE 29Chapter 1: Introducing Ajax var fragment = xsltProcessor.transformToFragment(xmlDoc, document); document.getElementById(“menuhere”).innerHTML = “”; document.getElementById(“menuhere”).appendChild(fragment); } XSLT is browser-specific, so you have one branch for IE and one branch for Firefox, but they both do the same thing. They store the XML response, they load an XSL style sheet, and they perform a transform on it. The fragment is then inserted into the page at the element with the ID “menuhere”. PAGE 30Chapter 1: Introducing Ajax an Ajax application. The discussion looked at why indiscriminate use of Ajax could have the reverse effect to the desired one. Examples showed some good Ajax applications in action, and the chapter discussed their techniques. Last, you created your own Ajax application that used a lot of the technologies to be discussed and used in this book. Exercises Suggested solutions to these questions can be found in Appendix A. 1. 2. |