Solved

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

Posted on 2007-03-31
4
555 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:aaron_karp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:stanscott2
ID: 18829178
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
 
LVL 5

Assisted Solution

by:NickVd
NickVd earned 50 total points
ID: 18829211
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
 

Author Comment

by:aaron_karp
ID: 18829303
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
 
LVL 15

Accepted Solution

by:
stanscott2 earned 450 total points
ID: 18830239
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question