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.

Communication Errors<br />

Chapter 14: Error Handling and Debugging<br />

With the introduction of Ajax programming (discussed in Chapter 17 ), it has become quite common for<br />

web applications to dynamically load information or functionality throughout the application ’ s lifecycle.<br />

Any communication between JavaScript and the server is an opportunity for an error to occur.<br />

The first type of communication error involves malformed URLs or post data. This typically occurs when<br />

data isn ’ t encoded using encodeURIComponent() before being sent to the server. The following URL,<br />

for example, isn ’ t formed correctly:<br />

http://www.yourdomain.com/?redir=http://www.someotherdomain.com?a=b & c=d<br />

This URL can be fixed by using encodeURIComponent() on everything after “ redir= ” , which produces<br />

the following result:<br />

http://www.yourdomain.com/?redir=http%3A%2F%2Fwww.someotherdomain.com%3Fa%3Db%26c%3Dd<br />

The encodeURIComponent() method should always be used for query string arguments. To ensure that<br />

this happens, it ’ s sometimes helpful to define a function that handles query string building, such as the<br />

following:<br />

function addQueryStringArg(url, name, value){<br />

if (url.indexOf(“?”) == -1){<br />

url += “?”;<br />

} else {<br />

url += “ & ”;<br />

}<br />

}<br />

url += encodeURIComponent(name) + “=” + encodeURIComponent(value);<br />

return url;<br />

This function accepts three arguments: the URL to append the query string argument to, the name of the<br />

argument, and the argument value. If the URL that ’ s passed in doesn ’ t contain a question mark, then one<br />

is added; otherwise, an ampersand is added because this means there are other query string arguments.<br />

The query string name and value are then encoded and added to the URL. The function can be used as<br />

in the following example:<br />

var url = “http://www.somedomain.com”;<br />

var newUrl = addQueryStringArg(url, “redir”,<br />

“http://www.someotherdomain.com?a=b & c=d”);<br />

alert(newUrl);<br />

Using this function instead of manually building URLs can ensure proper encoding and avoid errors<br />

related to it.<br />

Communication errors also occur when the server response is not as expected. When using dynamic<br />

script loading or dynamic style loading as discussed in Chapter 10 , there is the possibility that the<br />

requested resource is not available. Firefox, Chrome, and Safari fail silently when a resource isn ’ t<br />

returned, whereas IE and Opera both error out. Unfortunately, there is little you can do when using these<br />

techniques to determine that an error has occurred. In some cases, using Ajax communication can<br />

provide additional information about error conditions.<br />

485

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

Saved successfully!

Ooh no, something went wrong!