By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,325 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,325 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!

Mar 16 '07 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Mar 16, 9:16 am, "Brian Kendig" <b...@enchanter.netwrote:
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?
Mar 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.