0

I am a new learner of backbone.js and I tried to follow the hello world tutorial to just simply show a message on my screen. Following the tutorial video, I have a problem that the function console.log('hello world'); shows nothing. Here is my code:

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="UTF-8">
  <title> Learning about Bacjbone.js </title>
</head>
<body>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  <script type="text/javascript" src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  <script type="text/javascript">

    Person = Backbone.Model.extend({
      initialize: function() {
      console.log('hello world');
      }
    });
    var person = new Person();
  </script>
</body>
</html>

Did I miss something or did something wrong? Please give me some idea. Thank you very much for your patient and any advises.

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
user1687919
  • 1
  • 2
  • 4
  • What browser are you using? The console object is only available in browsers that have a javascript console. IE9+, Chrome, Safari, and Firefox to name a few have a console. console.log() outputs to the javascript log, it does not write text to the DOM anywhere. – Mark Sep 21 '12 at 06:15
  • i prefer the use of console.debug(), sometimes console.log() is not working – Moszeed Sep 21 '12 at 06:23
  • Thanks for your replying:) I tried IE9 and Chrome, and it did output what you said but I don't know how to show the message on the browser... I read Joe Zim's website(http://www.joezimjs.com/javascript/introduction-to-backbone-js-part-1-models-video-tutorial/) and just followed what he does in the video, but nothing happened. I tried and googled for that for many days...upset:( – user1687919 Sep 21 '12 at 06:27
  • Where to you expect the console output? As you're code is correct, maybe this will answer your question: http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function – Andreas Köberle Sep 21 '12 at 06:46
  • I just want to show the hello world string on the browser, since I am a starter of BackboneJS and this is my first step trying using backbone. Thank to your replying and I read the question then I tried, but not helped... – user1687919 Sep 21 '12 at 06:55
  • 1
    http://jsfiddle.net/JNzwC/ its working in chrome. check it. – Array out of bound Sep 21 '12 at 07:29
  • My original code DID work too... I just wondered why there is nothing output on my screen. Thank you anyway:) – user1687919 Sep 22 '12 at 10:22

2 Answers2

0

Since you are a new user, I can't tell your level of experience; but are you confusing:

console.log('hello world')

with:

document.write('hello world')?

If you want to check to see if that code is working, you can always change console.log to just alert

rkw
  • 7,287
  • 4
  • 26
  • 39
  • Thanks for your replying. I think I DO misunderstand what console.log does... Because I am also learning AngularJS now, and it provides a new way to trans variables like {{sth}}. So I thought console.log does the same thing, and is a new method of BackboneJS to show message... I think I should read more about console.log! – user1687919 Sep 21 '12 at 07:52
-1

Your code is absolutely correct. There may be some other error, I couldn't find out.

If it is browser specific as console.log works only in FF. Try to alert the message as below in jsfiddle demo.

Please Find the Demo of your working code: here

but Try running below working html+ java script code.

    <html>
    <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script> 
    <script type="text/javascript">
    (function($){  
    Person = Backbone.Model.extend({    
          initialize: function() {
                          console.log('hello world');
          }
        });
    var person = new Person();
      })(jQuery);
    </script>
    </head>
    <body>
    </body>
    </html>
Umesh Patil
  • 10,475
  • 16
  • 52
  • 80
  • 1
    Whats the point of your code. Its the same as in the question, but more complicated without any reason. There is no need to create a closure and passing jquery into it. – Andreas Köberle Sep 21 '12 at 06:44
  • @Umesh It makes no difference:( anyway thank to your replying!I am still trying too. How can I just print the message on my screen...it IS caught by the browser... – user1687919 Sep 21 '12 at 06:49
  • @AndreasKöberle, Sorry. Please Ignore the code. Just see the demo of working code. – Umesh Patil Sep 21 '12 at 06:52
  • @user1687919, Please Try to alert or as I did in the above jsfiddle demo. – Umesh Patil Sep 21 '12 at 06:52
  • @Umesh, It works and DOES show the message! Thank you very much! But how can I show the string through console.log() or somewhat? I mean by the way which is unique in Backbone? – user1687919 Sep 21 '12 at 07:00
  • @user1687919, great! Is console.log your favorite function ? If you only want to test the string or value, you can just find it by alerting or placing somewhere in HTML dom. If it works, you can accept the answer :) – Umesh Patil Sep 21 '12 at 07:05
  • 1
    @Umesh, actually I have no idea about what is my favorite or what since I just know about backboneJS for few days, haha. I could alert something in the Backbone.Model scope in my original code, but watching the tutorial video, he can just show the string through console.log('hello world'); but I couldn't... – user1687919 Sep 21 '12 at 07:12