0

I know that my question is so similar to this one, but I have the same problem and none of the solutions work for me.

My directory structure is the following:

static
    lib
        require.js
        jquery-1.11.3.js
        progressbar.js   
    client
        client.messages.js
        client.js
    main.js
    config.js

My HTML file is like this:

<script src="{{ url_for('static', filename='scripts/lib/require.js') }}"></script>

<script>
    var require = {
        baseUrl: "/static/script",
        paths: {
        /* Load common libraries from CDN and fallback to local versions. */
        "jquery":      "lib/jquery-1.11.3.min.js",
        "progressbar": "lib/progressbar.min.js",
        "messages":    "client/client.messages.js"
        }
    };
</script>

<script src="{{ url_for('static', filename='scripts/main.js') }}"></script>

I have also tried to make the configuration in the config.js file which was like:

var require = {
        baseUrl: "/static/script",
    paths: {
        /* Load common libraries from CDN and fallback to local versions. */
        "jquery":      "lib/jquery-1.11.3.min.js",
        "progressbar": "lib/progressbar.min.js",
        "messages":    "client/client.messages.js"
    }
};

and then the HTML file was like:

<script src="{{ url_for('static', filename='scripts/config.js') }}"></script>
<script src="{{ url_for('static', filename='scripts/lib/require.js') }}"></script>
<script src="{{ url_for('static', filename='scripts/main.js') }}"></script>

Further down in my HTML file there is this line:

<body onload="bodyLoaded()">

and this bodyLoaded() function is in my main.js:

var bodyLoaded = function () {
    //load scripts first
    var scripts = ['static/scripts/client/client.messages.js', 'static/scripts/client/client.js'];
    $.getScript(scripts[0], function () {
        $.getScript(scripts[1], function () {
            pluginCheck();
        });
    });
};

The error I get is:

Uncaught ReferenceError: $ is not defined

for this line in bodyload():

$.getScript(scripts[0], function () {

Am I missing something?

4
  • Have you tried to change $ for jQuery? Commented Jan 21, 2016 at 12:13
  • Sorry, can you make your point a bit more clear? What exactly can I change? Commented Jan 21, 2016 at 12:15
  • Try changing the code $.getScript(... to jQuery.getScript(... Commented Jan 21, 2016 at 12:17
  • No, there is (almost) the same error: "Uncaught ReferenceError: jQuery is not defined" Commented Jan 21, 2016 at 12:27

1 Answer 1

2

Well... you're loading RequireJS, you've configured it, but you are not using it.

One way to get what you want would be:

var bodyLoaded = function () {
    //load scripts first
    var scripts = ['static/scripts/client/client.messages.js', 'static/scripts/client/client.js'];
    require(['jquery'], function ($) {
        $.getScript(scripts[0], function () {
            $.getScript(scripts[1], function () {
                pluginCheck();
            });
        });
     });
};

I've added require(['jquery'], ... so that jQuery actually gets loaded.

This being said, it is very likely that this is not the solution you want to end with. For one thing, it seems to me that client.messages.js and client.js are candidates for being loaded with RequireJS so, really, RequireJS should be configured to find them (I see client.message.js in the config but not client.js). You'll most likely have to use shim to define the dependencies among them.

I suggest that you thoroughly read RequireJS' documentation.

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

1 Comment

Thanks a lot! That was the problem. I have not used client.js till now and that's why I have not loaded.

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.