"$ is not defined" in a site running on Prototype library

I'm using the Prototype framework for a fairly large mostly-Ajax-powered project.

The site's pretty much done, but the web developer toolbar keeps telling me I have an error:

"$ is undefined" on line 1

Often it appears multiple times per page. Since the content is dynamically loaded, the error shows up "on line 1" which is pretty useless.

I realize this is hard to troubleshoot without looking at all my code, but has anyone else experienced this before? Any idea where to look for the problem? It must be somewhere I try to call the $(id) prototype function, but where it hasn't been initialized yet? Maybe the library hasn't fully downloaded yet?
aaron_karpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stanscott2Commented:
In the Prototype library, the $ function is used to return an object reference from the string name:

function $(strName) {
// skip error checking
     return document.getElementById(strName);
}

If the function is undefined, EITHER:

the library isn't loaded yet  OR

you need to qualify the '$' with the library name (I'm not a current user of Prototype):

libraryname.$("someObjectName");

You might also have re-defined $ or set it to null.
0
NickVdCommented:
The problem is due to prototype not being loaded.  Make sure you that you included the prototype javascript before you attempt to call it, and that the file is intact, not corrupted and in the correct location.

Without seeing code, that's all anyone can suggest.
0
aaron_karpAuthor Commented:
Thanks guys. I'm guessing you're right, that the files aren't completed loaded when I try to access them.

If my page looks like:
<script src='prototype.js'></script>
<script>
var value = $('myValue').value;
</script>

Shouldn't that work every time? How do javascript files load into the page? Can other stuff run while they're still downloading? If so, do you have any suggestions?
0
stanscott2Commented:
No, it doesn't work every time.  In the code as you've given it, the second script has no pause or wait built in.  Because prototype is a large library, it hasn't finished loading the code runs.

There are five ways around this.

 If, for some reason, you have to have an object reference right then, don't use $.  Instead, use:  var val = document.getElementById('myValue').value;  This will always work.

If you're going to do some additional coding at the beginning, you can also put in a pause or wait for, say, 5-10 seconds before running the next line of code.

You can also put the code at the BOTTOM of the page, instead of directly under the first script.  Your browser will process all of the HTML before it runs the code at the bottom of the page.

You can put the code into the BODY tag, using an onload event.  This way, the entire page will load before the process starts.

I had a similar situation last year with one of my own Javascript libraries.  My solution at that time was to pull out the two functions that I absolutely needed at the beginning and put them into a separate library, so there was no delay in loading them.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.