Section 1.15. Exercise Solutions


1.15. Exercise Solutions

Just Do It Solution

Open up your boards.html file, and add a new JavaScript function called getBoardsSold(), right after createRequest(). Then, see if you can add a line of JavaScript in getBoardsSold() to create a new request object (that's Step "a" from page 18).

     <script language="javascript" type="text/javascript">      var request = null; All this is the pre-assembled javaScript you should have type in.      function createRequest() {        try {          request = new XMLHttpRequest();        } catch (trymicrosoft) {          try {            request = new ActiveXObject("Msxml2.XMLHTTP");          } catch (othermicrosoft) {            try {              request = new ActiveXObject("Microsoft.XMLHTTP");            } catch (failed) {              request = null;            }          }        }        if (request == null)          alert("Error creating request object!");      } Here's the start of your getBoardsSold() function...      function getBoardsSold() { ...which uses createRequest() to get a new request object created.        createRequest();      }     </script> 


WHO AM I

It's time to test those higher-order thinking skills. There's an important character in the Ajax play that you haven't met yet, and he's ready to make his grand entrance. See if you can upstage this new character by figuring out who he is before you turn the page. We've helped you understand what each quote refers to with a few notes below.

"Singing? Well, a lot of people don't realize this, but I am classically trained. And I love Wagner."

Here's the Opera web browser bragging a bit about her singing chops. Surfing... browsers are all

"Give me your tired, your poor, your huddled brackets yearning to be styled."

That's definitely a web browser, asking for HTML content.

"Sure, I love water sports. In fact, I was an extra in 'Point Break', and almost got a lead role in 'Blue Crush'."

Surfing... browsers are all about the surfing.

Netscape vs. IE, anyone? Remember the browser wars?

"I'm as tough as they come... I've even been involved in a few wars over the years."

WHO AM I?

The Web Browser


Work It Through- Answer

We're going to need a couple of JavaScript functions to turn Katie's report into an Ajax-powered app. Below are the names of three JavaScript functio ns. Draw a line connecting each function name to what you think it will do in the final version of the Boards app.

                                                  Create a new object for                                                  talking to the web server. getBoardsSold()                                                  Ask the server for the latest                                                  snowboard sales figures. updatePage()                                                   Set the number of boards sold                                                  and the cash that Katie's made                                                  to the most current values. createRequest()  


Work It Through- Answer

You've alre ady seen two of the properties from the request object we've been using; now it's time to put what you've learned to the test. Below on the left are several properties of a request object, and on the righ t are de scrip tions of these properties. See if you can match the property name to its purpose.

                                             The HTTP status code                                             returned from the server. response Text                                             The function for the browser to                                             run when the server responds to                                             your request. readystate                                             A number indicating what state                                             the request is in: loading, in                                             progress, finished processing, etc. onreadystatechange                                             The data returned from                                             the server in response to                                             your request. status 





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net