04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14: Error Handling and Debugging<br />

This type of error typically occurs when JavaScript is being dynamically generated by a server - side<br />

component. Many server - side languages automatically insert HTML into the output if a runtime error<br />

occurs, and such output clearly breaks JavaScript syntax. If you ’ re having trouble tracking down a<br />

syntax error, double - check each external JavaScript file to be sure that it doesn ’ t contain HTML inserted<br />

by the server because of an error.<br />

The System Cannot Locate the Resource Specified<br />

Perhaps one of the least useful error messages is “ The system cannot locate the resource<br />

specified. ” This error occurs when JavaScript is used to request a resource by URL and the URL is<br />

longer than IE ’ s maximum URL length of 2083 characters. This URL length limit applies not just to<br />

JavaScript but to IE in general (other browsers do not limit URL length so tightly). There is also a URL<br />

path limit of 2048 characters. The following example causes this error:<br />

function createLongUrl(url){<br />

var s = “?”;<br />

for (var i=0, len=2500; i < len; i++){<br />

s += “a”;<br />

}<br />

}<br />

return url + s;<br />

var x = new XMLHttpRequest();<br />

x.open(“get”, createLongUrl(“http://www.somedomain.com/”), true);<br />

x.send(null);<br />

In this code, the XMLHttpRequest object attempts to make a request to a URL that exceeds the maximum<br />

URL limit. The error occurs when open() is called. One workaround for this type of error is to shorten<br />

the query string necessary for the request to succeed, either by decreasing the size of the named query<br />

string arguments or by eliminating unnecessary data. Another workaround is to change the request to a<br />

POST and send the data as the request body instead of in the query string. Ajax, the XMLHttpRequest<br />

object, and issues such as this are discussed fully in Chapter 17 .<br />

Debugging Tools<br />

JavaScript debugging has come a long way from its roots using alert dialogs. IE, Firefox, and Safari all<br />

support full - featured JavaScript debuggers that include stack information, variable inspection, and<br />

stepping through scripts. As of version 9.5, Opera does not have a JavaScript debugger.<br />

Internet Explorer Debugger<br />

As of version 8, IE ships with a JavaScript debugger as part of its development tools. By default,<br />

JavaScript debugging is turned off, so you must first enable it. To do so, click the Tools menu and go into<br />

Internet Options. Click on the Advanced tab, and make sure that the check box next to “ Disable script<br />

debugging (Internet Explorer) ” is unchecked. Click OK to exit the dialog box.<br />

496

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!