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!
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!