04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Error Handling and Debugging<br />

In IE 8, Firebug, Chrome, and Safari, the message display on the error console differs according to the<br />

method that was used to log the message. Error messages contain a red icon, whereas warnings contain a<br />

yellow icon. Console messages may be used, as in the following function:<br />

function sum(num1, num2){<br />

console.log(“Entering sum(), arguments are “ + num1 + “,” + num2);<br />

console.log(“Before calculation”);<br />

var result = num1 + num2;<br />

console.log(“After calculation”);<br />

}<br />

console.log(“Exiting sum()”);<br />

return result;<br />

As the sum() function is called, several messages are output to the JavaScript console to aid in<br />

debugging. The Safari JavaScript console can be opened via the Develop menu (discussed earlier); the<br />

Chrome JavaScript console is opened by clicking the “ Control this page ” button and selecting Developer<br />

JavaScript console (also discussed earlier); and the Firebug console is accessed by clicking the icon in the<br />

lower - right corner of the Firefox status bar. The IE 8 console is part of the Developer Tools extension,<br />

which is available under the Tools menu; the console is on the Script tab.<br />

Opera ’ s JavaScript console is accessible using the opera.postError() method. This method accepts a<br />

single argument, the message to write to the console, and is used as follows:<br />

function sum(num1, num2){<br />

opera.postError(“Entering sum(), arguments are “ + num1 + “,” + num2);<br />

opera.postError(“Before calculation”);<br />

var result = num1 + num2;<br />

opera.postError(“After calculation”);<br />

}<br />

opera.postError(“Exiting sum()”);<br />

return result;<br />

The opera.postError() method can be used to write out any type of information to the JavaScript<br />

console, despite its name.<br />

Another option is to use LiveConnect, which is the ability to run Java code from JavaScript. Firefox,<br />

Safari, and Opera all support LiveConnect and may interact with a Java console. It ’ s possible to write<br />

messages to the Java console using JavaScript via the following code:<br />

java.lang.System.out.println(“Your message”);<br />

489

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

Saved successfully!

Ooh no, something went wrong!