469,306 Members | 1,982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

'Object' is undefined

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/J...ect/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
Jul 20 '05 #1
7 20051
In article <4d**************************@posting.google.com >, sa**@freeddns.org
(sams) writes:
Appreciate any help to resolve this problem.
<head>
<script language="JavaScript" src="calendar.js"></script>
</head>

<script> obj1 = new Calendar(2, 2005); </script>
<script> obj2 = new Calendar(7, 2001); </script>


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.
--
Randy
Jul 20 '05 #2
hi************@aol.com (HikksNotAtHome) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
In article <r8**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
writes:
hi************@aol.com (HikksNotAtHome) writes:
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.


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

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.

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.
--
Randy
Jul 20 '05 #4
hi************@aol.com (HikksNotAtHome) writes:
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;}
-->
<h1>Header</h1>
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
In article <ll**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
writes:
hi************@aol.com (HikksNotAtHome) writes:
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.


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


I think it was NS6 but not absolutely positive. Its been about a year or so,
and didn't really worry with it anymore, just moved it to the onload and went
on as a happy camper :)
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.


necessary or desirable? I can't see where the browser could know that it would
be necessary to wait. If it could, it could be assumed that the browser should
know to wait for the page to load before executing scripts, as it may need an
element in the body that doesn't exist yet. But I can see the logic behind not
executing more script until the last script loads though.
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.


Isn't it just grand trying to debug code you can't see? <g>
--
Randy
Jul 20 '05 #6
"HikksNotAtHome" <hi************@aol.com> wrote in message
news:20***************************@mb-m06.aol.com...
In article <ll**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
<snip>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.
Jul 20 '05 #7
> 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
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by s_m_b | last post: by
49 posts views Thread by matty | last post: by
16 posts views Thread by Java script Dude | last post: by
17 posts views Thread by yb | last post: by
7 posts views Thread by Ike | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.