4

I am new to KnockOut js.When I tried a simple Hello World Example in Visual Studio 2012 ,I am getting an runtime exception saying "Javascript Run time error:ko is undefined".Please help me

this is my code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>App1</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <!-- App1 references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
    <script src="/js/knockout-3.0.0.js" type="text/javascript"></script>
    <script src="/js/jquery.min.js" type="text/javascript"></script>


</head>
<body>
    <p>Hello, <span data-bind="text:name"></span>!</p>
    <script>
        ko.applyBindings({name:ko.observable('World')});  
    </script>
</body>
</html>
Rashad Valliyengal
  • 3,132
  • 1
  • 25
  • 39
  • You didn't include ko properly. How do you reference it in your page ? Where do you use ko (on the onready event) ? – Damien Nov 28 '13 at 10:06
  • What is _default.js_? Do you reference `ko` from that file? If so, you should include it **after** you add _knockout-3.0.0.js_. – Alex Filipovici Nov 28 '13 at 10:29

2 Answers2

4

Make sure you are using the correct reference number in all your references e.g If you installed knockout 3.1.0 from nuget and still using 2.2.1 in references then it can cause this error. I fixed that error by making sure that all my references matches the version I installed.

2

The error should become from the included javascript. May be they aren't available on the server. The follow snippet works.

<html>
<head>
    <meta charset="utf-8" />
    <title>App1</title>
    <script src="http://knockoutjs.com/downloads/knockout-3.0.0.debug.js" type="text/javascript"></script>
</head>
<body>
    <p>Hello, <span data-bind="text:name"></span>!</p>
    <script>
        ko.applyBindings({name:ko.observable('World')});  
    </script>
</body>
</html>
Damien
  • 8,889
  • 3
  • 32
  • 40
  • 1
    How do you know that the error occurs in the posted code and not in _default.js_? I believe that this scenario is more likely to happen... – Alex Filipovici Nov 28 '13 at 10:34
  • Because the OP mention only one error error : `Javascript Run time error:ko is undefined`. So I think there is no error in the default. – Damien Nov 28 '13 at 10:37