0

I have defined the following module in MyModule.js:

MyModule.js:

MyModule = {

    controller: {
       paintCar: function(color){
            //PAINTING PROCESS
       }
    },

    tester: {
        ...
    },

};

Then, I have another javascript file, other.js:

other.js:

MyModule.controller.paintCar('red');

My index.html

<body>
    <script scr="MyModule.js"></script>
    <script src="other.js"></script>
  </body>

All these files are put under WebContent directory of Eclipse Dynamic Web Project.

In other.js when I try to run the above code, I got error "MyModule is not defined". Why?

2 Answers 2

1

You misspelt src here: <script scr="MyModule.js">. This would have been picked up by basic automated QA testing.

(Original answer before the theory was confirmed) Presumably, because you aren't loading the script files properly. Since you haven't shown us the code that does that (or even told us what environment you are using) it is hard to say exactly how you went wrong.

Assuming you are using client side JS in a webpage, your code should look something like:

<script src="MyModule.js"></script>
<script src="other.js"></script>

Order is significant. End tags are mandatory. Self-closing tag syntax is unacceptable (unless the document is served as application/xhtml+xml)

(This uses HTML 5 syntax. For HTML 4 / XHTML 1, add a type attribute. For HTML 3.2, add a language attribute)

6
  • It is the HTML that causes the scripts to be loaded, and you haven't shown us any of that. (he said, in response to a comment that has since been deleted)
    – Quentin
    Apr 15, 2011 at 10:33
  • I updated my post. I have loaded the js files in my index.html
    – Mellon
    Apr 15, 2011 at 10:38
  • Updated answer. See first line.
    – Quentin
    Apr 15, 2011 at 10:41
  • By the way, in JS, is the module name and the js file name must be the same or not?
    – Mellon
    Apr 15, 2011 at 10:46
  • @Mellon: The filename is not related to the "module" name at all. JavaScript doesn't have modules, just objects. Apr 15, 2011 at 10:48
1

You haven't mentioned your environment, but generally you have to ensure that the JavaScript in MyModule.js is executed before the JavaScript in other.js, because you need MyModule.js to set up the MyModule variable.

How you do it will vary by environment. In a web browser, you'd do that by putting in two script tags, first one for MyModule.js then the one for other.js (although frequently, for use on websites, you want to have a build process that combines your scripts into a single file to minimize HTTP requests). In NodeJS, there's the whole require mechanism.

Update based on your edit:

Looks like a typo:

<body>
    <script scr="MyModule.js"></script>
             ^^-- here, they're transposed
    <script src="other.js"></script>
</body>

If that typo isn't really in your file, then look to see that the files are where the web server expects, that the web server isn't being thrown by capitalisation, that sort of thing. Fundamentally, that's correct.

2
  • I updated my post. I have loaded the js files in my index.html
    – Mellon
    Apr 15, 2011 at 10:38
  • Yes, it is a typo. Thank you:)
    – Mellon
    Apr 15, 2011 at 10:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.