AJAX In Action [239]
Another feature is that we can easily take a table layout that a web designer has designed and place it into an XSLT file. If we ever need to make a change such as swapping columns, it is as easy as cutting and pasting the data. No more scratching our heads wondering if the tags are still going to be right when we paste them. By using XSLT, we are removing the processing of the HTML from our dynamic code. This allows us to change the results table without any major problems. And finally, using JavaScript lets us do some things very easily that we couldn’t do if we did the transformation on the server:
Licensed to jonathan zheng 490 CHAPTER 12 Live search using XSLT ■ We can retrieve different XSL documents based on a theme, screen dimensions, language, and so on. ■ We can retrieve an XML document and an XSL document without help from the server. ■ We can examine an XML log file on our local machine without having control over the XML document structure. When performing your daily tasks, you’ll find that Ajax gives you so many possibilities. We still have one issue with the live search that we need to address: allowing the user to bookmark the results page. 12.5.4 Overcoming the Ajax bookmark pitfall There is one downside to using Ajax to perform searches: Bookmarking the page in the traditional manner is not an option. (This same problem occurs with frames and pop-up windows.) Bookmarks allow us to come back to the search results in the future without having to type in the request information, and they can be easily sent to friends and colleagues by email or messaging. Since an Ajax search does not require a postback to the server, we are not changing the URL of the page during the search, and therefore bookmarking the URL will simply mark the starting point for our application, not the results that we want to preserve. An easy solution for this is to add a behavior that lets us remember the search. We can build a dynamic string that will be used to create a dynamic bookmark. This dynamic string will contain a direct link to the page it’s on and will include a querystring parameter with the search value. Therefore, when we write this string to the page to form the link, the user can either bookmark it (by rightclicking on the link) or copy the link, and her search will be automatically saved. We add this functionality of reading the querystring value when the page is loaded after we build the link. The link can be built when our GrabNumber() function is executed. We add another span to our document so that we have a location to put this link on the page. In this case, the span has an ID of spanSave, as you can see in listing 12.11 by looking at the statement where getElementById is invoked. We can position the span wherever we want on the page so it is convenient for the user. Listing 12.11 Altering the GrabNumber function to integrate a bookmarking link function GrabNumber(){ var strLink = "Save Search"; document.getElementById("spanSave").innerHTML = strLink; The code in listing 12.11 generates a dynamic link to our current search page and adds the querystring