Javascript – Print an object in console

The toSource() function, which we talk about yesterday(Javascript – Print an object with alert()), only works in Firefox, a more proper way which work in non Gecko engine browser like Chrome and Safari is using console.

console.log(yourObject);
console.info(yourObject);
console.warn(yourObject);
console.error(yourObject);

 

Try the following example.

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        myWebsite = new Object();
        myWebsite.name = 'Eureka!';
        myWebsite.url = 'https://ykyuen.wordpress.com/';

        console.log(myWebsite);
        console.info(myWebsite);
        console.warn(myWebsite);
        console.error(myWebsite);
      });
    </script>
  </head>
  <body>
    <p>Eureka!</p>
  </body>
<html>

 

See what you get in Firebug.

Done =)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.