'Object' is undefined

S

sams

Iam trying to make the devshed tutorial for JavaScrip Object lesson to
work in my PC. The calendar.js file has the object 'Calendar'
constructed but when I called using the below html doc, I get the
"Calendar' is undefined error for line 6 and 7, where the obj1 and
obj2 are specified.
The full code of the calendar.js is in
http://www.devshed.com/Client_Side/JavaScript/Object/page9.html

Both the files are in the same directory. Iam using Windows 2000
Professional with IE6.

Appreciate any help to resolve this problem.

<html>
<head>
<script language="JavaScript" src="calendar.js"></script>
</head>
<body bgcolor="white">
<script> obj1 = new Calendar(2, 2005); </script>
<script> obj2 = new Calendar(7, 2001); </script>
</body>
</html>


TIA

Sam
 
L

Lasse Reichstein Nielsen

Its a timing issue. You are trying to call the new Calendar() function before
the calendar.js file has loaded to the point that it exists. Try using the
onload to fire them off, and it should solve the undefined object errors.

That is unlikely. The script is loaded and executed before parsing
continues. Some browsers allow parsing to continue earlier by adding
the defer attribute to the script tag, but it will still stop at the
next non-defer script tag.

A more likely problem is that "calendar.js" is a verbatim copy of the
script on the referred page ... including the <script> tag.

In an external JS file, you must not have
<script language="JavaScript">
at the beginning and </script> at the end. That will give syntax errors
that will prevent the file from being executed.

The page at devshed.com is slightly misguiding in that it gives the
code with <script> tags around, but later gives code that uses it
as an external file.

/L
 
L

Lasse Reichstein Nielsen

So if I load 100 .js files, then nothing will happen until all 100
of those files are loaded? Or am I understanding wrong?

That is correct. You can check it with only one JS file where you do
something boring like counting to a hundred million.

<h1>Header</h1>
<script type="text/javscript" src="longjs.js"></script>
<!-- script contains
for (var i=0;i<100000000;i++) {var x=i;}
-->
I know the onload event handler won't fire until the entire page is
loaded, but I seem to recall having problems where I was trying to
call functions that had not been downloaded from an external .js
file in the past. Using the body's onload to call them solved my
problems.

If you could remember which browser you were using, it's worth
testing.

Waiting for the scripts to finish is actually necessary, since they
can contain document.write's, and even document.write's writing new
script tags that can define functions that the next script tag will
need.
Although, with regards to the OP, I didn't look at the .js file to
see if it had the most common error - the script tags in it.

It's a guess. He didn't show us his .js file, but from the description
he seemed to be following, it is a likely mistake.

/L
 
R

Richard Cornford

HikksNotAtHome said:
It seems the one I used to test it was a simple array file that
had 30k or so entries, and tried to process it before the page
loaded and got errors (NS6 I think, but don't remember for sure).
Since NS6 is basically old now, it may be something that got
changed in NS7, or, I could be remembering the browser wrong.
<snip>

I remember having timing problems with Netscape 6.2 loading a 90k array
in a JS file, so I think you have the right browser.

Richard.
 
S

sams

In an external JS file, you must not have
<script language="JavaScript">
at the beginning and </script> at the end. That will give syntax errors
that will prevent the file from being executed.

Thank you, I removed the <Script></script> tags from the .js file and
no other change. It worked. Great.

Apprecite all your help.

Sam
 

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,808
Messages
2,569,684
Members
45,438
Latest member
greenleaf

Latest Threads

Top