473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='m yFunction();'" 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.myMeth od = 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 3315
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='m yFunction();'" 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.myMeth od = 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
5197
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 to wait for the external resource to load to show up the page, thus giving a perceiving faster load time for the user.
1
1334
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 Path="webServices/securityService.asmx" />
4
3164
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 executes. Help! I can't figure this out. The external JS file is "members.js". Here's the code:
1
3801
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 = document.createElement("SCRIPT"); newScr.src = "newScr.js"; newScr.type="text/javascript";
5
2921
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 that validates at w3c 2. I use: <script language="JavaScript" type="text/javascript" src="dashboard.js" ></script>
4
2639
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 IE6 and IE7) and online in IE7, but frustratingly, online in IE6, the javascript does not render on the page, unless you refresh the page. Is there something different about the way IE6 handles sessions? Why
3
2052
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 attribute? Can't seem to get this to work. Also, if there is a way, will the browser cache any dynamically loaded scripts?
2
1515
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 me to find the answer
8
5084
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 changed it to <script language="JavaScript"src="../include/common.js"></script> but still not working
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.