0

I have a function "loadDays()" that should be executed when the body-section is loaded:

<body onload="loadDays()">

In <script> I have my function loadDays():

<script>
    function loadDays() {
        // code
    }

Now I added src="https://requirejs.org/docs/release/2.3.5/minified/require.js" to the <script>-tag, to make use of require inside my function.

While it does recognize require then, it throws a new error:

"Uncaught ReferenceError: loadDays is not defined

at onload

What am I missing?

2 Answers2

2

If <script> tag has src attribute, then any content inside this tag is ignored.

You should add 2 separate tags:

<script src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
<script>
    function loadDays() {
        // code
    }
</script>

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

I've checked requireJS, so you should actually do it like this, as they said in the docs:

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             scripts/main.js after require.js loads. -->
        <script data-main="scripts/main" src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

Then add a main.js file in the scripts folder, and put your function inside of it.

    function loadDays() {
        // code
    }
Vedran
  • 143
  • 7