1

I am using the following code Html to load the scripts that I need. I am getting $ as Undefined. It is not able to load the jquery.

How can I get this to work.

<html>
<head>
<title>Using The Text Plugin With RequireJS</title>
<script  data-main="Scripts/init type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.6/require.min.js"></script>
<script type="text/javascript" language="javascript">

    require(['jquery', 'knockout-2.2.1', 'Template', 'text!Template.htm'], function ($, ko, t, temp) {

        $("body").append(temp);
        //make this new template engine our default engine
        ko.setTemplateEngine(t.myExternalTemplateEngine(t.templates));

        ko.applyBindings(t);

    });
</script>
</head>

code in Init.js in a separate file

require.config({
baseUrl:'Scripts',
paths: {

    'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js',
    'knockout-2.2.1': '//cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js',
    'knockout.mapping-latest': '//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.3.5/knockout.mapping.js',
    'Template': 'Template',
    'text': '//cdnjs.cloudflare.com/ajax/libs/require-text/2.0.5/text',
    'domready': '//cdnjs.cloudflare.com/ajax/libs/require-domReady/2.0.1/domReady.js'
   }

});
2
  • Did you try to use jQuery instead of $ sign ? Commented Jul 8, 2013 at 13:40
  • You might be running into the issue that your init.js is not loaded before your script that uses it. See this answer. What other errors do you have in the console? Commented Jul 8, 2013 at 13:47

1 Answer 1

2

Remove the .js from your paths configuration:

paths: {
    'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery'
}

More detail in this example repo

Sign up to request clarification or add additional context in comments.

2 Comments

good catch -- didn't see that. But regardless, the script loading approach is going to hit some random errors based on timing. See question I linked above.
Good point, wrapping it in a require(['init']) will stop the issue with race conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.