Questions about external javascript files - why is this not working?

B

Brian Kendig

I've got some questions about using an external javascript file,
included via '<script type="text/javascript" src="mycode.js" ></
script>':

(1) If I define a function "myFunction()" in the original HTML file,
and reference it by "onMouseDown='myFunction();'" in a tag, it works.
But if I move the myFunction() definition to an external javascript
file, Firefox tells me "myFunction is not defined". Why is this, and
how can I be able to define functions in an external file?

(2) Why do some people put "(function() {" at the very top of an
external javascript file, and "}) ()" at the very bottom? What does
this do, exactly - what's the benefit?

(3) I've seen some people do that when they define class methods in an
external javascript file, like "myClass.myMethod = function(blah)
{ ... }", and then after they include this in the main HTML file they
then define myClass. But if I include this JavaScript file that uses
myClass before I define myClass, Firefox gives me errors saying that
myClass is undefined. What are people doing to be able to include a
file that uses a class before the class is defined?

Thanks in advance for any advice!
 
G

Geoff Stearns

I've got some questions about using an external javascript file,
included via '<script type="text/javascript" src="mycode.js" ></
script>':
(1) If I define a function "myFunction()" in the original HTML file,
and reference it by "onMouseDown='myFunction();'" in a tag, it works.
But if I move the myFunction() definition to an external javascript
file, Firefox tells me "myFunction is not defined". Why is this, and
how can I be able to define functions in an external file?

Usually the problem is the path to the file is bad. Try checking the
case of the file name, and make sure the file is there and can be read
by the browser.

(2) Why do some people put "(function() {" at the very top of an
external javascript file, and "}) ()" at the very bottom? What does
this do, exactly - what's the benefit?


This is a popular method of defining a function and calling it at the
same time. Very useful for implementing things like singleton patterns
in js.

(3) I've seen some people do that when they define class methods in an
external javascript file, like "myClass.myMethod = function(blah)
{ ... }", and then after they include this in the main HTML file they
then define myClass. But if I include this JavaScript file that uses
myClass before I define myClass, Firefox gives me errors saying that
myClass is undefined. What are people doing to be able to include a
file that uses a class before the class is defined?

You need to defined the class before you use it. - but maybe they are
'using' it in other functions that aren't being called until after the
class is defined?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top