Section 2.19. Just Do It


2.19. Just Do It

Now that you've learned about static JavaScript, creating request objects, and better error handling, you need to make some more changes to pizza.html. Open up your HTML, and move all the code in createRequest() outside of the function, right after the line of JavaScript that looks like this:

     <script language="javascript" type="text/javascript">     var request = null;All the code that is in createRequest() should go here.     </script> 

Next, remove the createRequest() function altogether, and also remove the line in getCustomerInfo() that ran the createRequest() function. You can test your changes by commenting out the JavaScript that creates a request object for the type of browser you're using. If there are any errors, they should show up immediately, before you get a chance to use the Break Neck order form. Once you're done testing, remove all your comments, and make sure pizza.html runs on both Microsoft and non-Microsoft browsers. Save your changes, and you're ready to turn the page.

Any errors get reported before the order form ever gets loaded. Much better!

IE reports this error over a blank page, since it hasn't loaded all the HTML yet.

Frequently Asked Questions?

Q:

So the browser runs any JavaScript that's not in a function before the HTML in the page is displayed?

A:

Any JavaScript that's in the <head> section of your HTML will get run before the page loads. However, you can insert JavaScript anywhere in an HTML page... even between elements like <p> or <form> in the middle of the page. JavaScript in those sections runs when the browser gets to that part of the page. But, all static JavaScript will get run before anyone can actually use your page, and that's what's important here.

Q:

And tell me again why we have to use ActiveXObject to make Break Neck work on Internet Explorer?

A:

Remember the browser wars? It's still common to have the same object called different things in different browsers. Fortunately, Internet Explorer 7.0 is supposed to move to a standard naming scheme, and replace their ActiveXObject object with a new object, called XMLHttpRequest. Of course, you'll still have to support those older versions of Internet Explorer, so your code won't change too much.





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