2.18. Supporting multiple browsersIt's time to break into this pre-assembled JavaScript, and figure out exactly what's going on. Let's walk through exactly what each piece of createRequest() does, step-by-step.
ActiveXObjectWARNING! IE 5 on the Mac still won't work, even with this IE-specific code. If you're using IE on the Mac, though... well, what can we say? requestHere's our request variable again. 2.18.1. Now put it all together...var request = null; function createRequest() { try { request = new XMLHttpRequest();For non-Microsoft browsers like Safari and Firefox. } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = null;For the Internet Explorer fans. } } } if (request == null) alert("Error creating XMLHttpRequest!");Error handling in case something went wrong. } Don't forget about the big picture... we're still working on getting a request to the Break Neck web server.
I just tried this, and it really sucks that I had to enter my phone number in to find out that things weren't working. Couldn't you have told me that before I bothered typing my number in? 2.18.2. Don't annoy the customer!In the Break Neck app, customers only have to fill out one field before getCustomerInfo() gets called, and your code tries to create a new request object. Then, if something's wrong, the customer gets an error message. But imagine how annoying it would be to fill out an entire form, and then find out that something's wrong... what a waste of time! It looks like we need to find a way to let users know about any problems much earlier... like before they start typing into the Break Neck form.
Well, we've got to run the createRequest() function somehow, right? How else can we create the request object ? 2.18.3. JavaScript doesn't have to be in a functionRemember how we declared the request variable, but didn't put that line of code in a function? <head> <title>Break Neck Pizza Delivery</title> ... <script language="javascript" type="text/javascript"> var request = null;This code will run automatically when the page loads. </script> </head> Any JavaScript in your web page that's not in a function gets run statically. That means that as the web browser is loading your page, it will automatically run any JavaScript it finds that is not in a function, before anyone can type into your form or click any buttons on the page. So if we move all of the code in createRequest() out of a function, all the code that tries to create the request object will run as soon as the Break Neck form loads. If there are errors, customers will know right away... and getCustomerInfo() won't need to run createRequest() anymore-the request object will be ready to use, or the customer will have already received an error message. requestBy the time the page is loaded, a new variable, called request... Request Object...should be pointing at an instance of a JavaScript request object.The request variable justs holds something... in this case, a reference to a request object. |