14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

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

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

APPENDIX A ■ Debugging <strong>JavaScript</strong><br />

Sequential Uncommenting<br />

Another easy way to trace an error is to comment out the whole script <strong>and</strong> uncomment it function by function or—if<br />

it is a single function—line by line. Test the script every time you uncomment a line by reloading it in a browser, <strong>and</strong><br />

you will quickly get to the one that causes the error. This can take time, though, <strong>and</strong> it would be much easier to know<br />

roughly where the error occurs, which is why you need to rely on your browser to give you this information.<br />

Built in Developer Tools<br />

Debugging code in browsers has been greatly improved. Now all browsers have developer tools built in that give you<br />

the ability to inspect, update, <strong>and</strong> save code directly from the browser. But first, how do you access the developer tools<br />

in each browser.<br />

Microsoft Internet Explorer<br />

Internet Explorer exposes its developer tools if you click on the gear icon on the top-right corner of the browser <strong>and</strong><br />

then select “F12 developer tools.” You can also press F12 on your keyboard to get the tools to appear. (See Figure A-4.)<br />

Figure A-4. The F12 developer tools in Microsoft Internet Explorer<br />

One thing you will notice <strong>with</strong> the built-in tools is that they have many similar features. These features include<br />

a panel to inspect each part of your page, HTML, CSS, <strong>and</strong> <strong>JavaScript</strong>, as well as a profiler <strong>and</strong> a network monitor.<br />

In addition to all of these features is the console where you can write <strong>JavaScript</strong> directly.<br />

Safari<br />

The debugging tools in Safari are not available by default. To turn them on, go to Safari, Preferences, Advance <strong>and</strong><br />

then select “Show Develop menu in menu bar.”<br />

352<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!