473,378 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
1 3298
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
1
by: sales | last post by:
Hi Does ATLAS support the use of external javascript va this syntax --myPage.aspx-- atlas:ScriptManager ID="ScriptManager1" runat="server"> <Services> <atlas:ServiceReference...
4
by: dennise9 | last post by:
My exeternal JS works fine when the call is placed in the page head or body. But if I call the same javascript from a (clicked) text link on the page, the script throws a JS error when it...
1
by: ozzy.osborn | last post by:
Hello All, I have been struggling with a cross browser solution to loading external javascript files on the fly. I have been successful using the following code in IE6: var newScr =...
5
by: steven.harms | last post by:
I'm *sure* this has to be a FAQ and I see discussions of this in the clj archives, but I don't see the answer. Perhaps someone here can point the clueless, but... 1. I create an HTML document...
4
by: fmaxwell | last post by:
Dear Group I have a very frustrating problem. I have been trying to make it more difficult to access external javascript files by using PHP sessions. This works beautifully locally (both in...
3
by: battle.chris | last post by:
Hello. I'm using Prototype's Updater class to refresh sections of a page. Is there a way to include an external javascript files in the updated content; i.e., have a javascript tag with src...
2
by: sssunny27 | last post by:
my question is:- the use of external javascript files has been supported in Netscape Navigator browser since which version? options are:- a) version 3.0 b) version 4.0 can somebody help...
8
by: kkshansid | last post by:
external javascript file not working on server <script type="text/javascript" src="../include/common.js"></script> while internal wrking fine <script language="JavaScript"> . . </script> i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.