0

I'm trying to reuse some script that I have working on another page, but am consistently getting an error with this one.

I have a .js file called CalendarPopup.js. It's linked to my HTML like this:

<script type="text/javascript" src="/administrator/components/com_arrcard/js/CalendarPopup.js"></script>

It's a javascript library that displays a popup calendar the user can choose a date from when they click a link. The value then gets put into a field. Here's the code that displays the field and link:

<tr id="birthDate">
<td align="left" valign="top">
    <script type="text/javascript">
    var calStart = new CalendarPopup("calendar");
    calStart.showNavigationDropdowns();
    </script>
    Birth Date:
</td>

  <td>
    <input style="width:124px;" type="text" name="birthdate" value="" />
    <a href="#" onclick="calStart.select(document.instantForm.birthdate,'anchorBirthdate','MM/dd/yyyy'); return false;" id="anchorBirthdate">select</a>
  </td>
</tr>

Then there's a div that actually holds the calendar:

<div id="calendar" style="position:absolute;visibility:hidden; background-color:white; layer-background-color:white;"></div>

All of this works flawlessly on one page, but on this new one, I'm getting the error

CalendarPopup is not defined

on this line:

var calStart = new CalendarPopup("calendar");

What am I missing? I'm sure it's something really obvious, but I've been staring at it for too long and just can't see it.

10
  • 2
    Are you certain you have included the JS file in the new file? Commented Nov 16, 2010 at 18:22
  • 1
    Make sure you can see the CalendarPopup file in a browser/FireBug Commented Nov 16, 2010 at 18:23
  • @Oded, yes I'm sure. @wajiw, when I do a view source and click on the link in the script tag, it does open the CalendarPopup.js file. Commented Nov 16, 2010 at 18:29
  • are you including any other JS files on this page that are not loading correctly? Commented Nov 16, 2010 at 18:30
  • 1
    What is the reason behind to write script tag inside <td> Commented Nov 16, 2010 at 19:20

4 Answers 4

1

Make sure the script that defines the CalendarPopup class is in a place where it is executed before any other javascript references it.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm thinking this must be the problem. To elaborate, EmmyS will want to use the <body onload=''> event, or, event better, one of the popular libraries out there document 'ready' events.
I can't use any body events; this is inside a Joomla site, and the head and body tags are generated, not coded inside the page I'm working in.
@EmmyS I know some libraries out there support document 'ready' events that will fire a the correct time regardless as to when they are specified. I know jQuery does this.
0

If the source of your js file is the same as http://www.mattkruse.com/javascript/calendarpopup/source.html, then it is said in that you must include the three other js files too
AnchorPosition.js
date.js
PopupWindow.js

Thanks.

1 Comment

Read a bit further; it also says "Or, you may choose the "Combined" source option above to include all required files in a single JS file." We're using the combined file, which contains all the code necessary. And as I said, this works in other sections of the site.
0

You could add an event listener to the window and execute the code that uses the .js files after they've been loaded.

//Some js file

window.addEventListener('load',onload,false);


function onload(){

 var x = new yourObject();
}

You're currently calling it before it's been loaded, hence the object not defined error.

Comments

0

The problem is more than likely the load order of your javascript file.

Place the <script> inclusion tag in in the <head> and you will be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.