Jump to content

"Not a function" error


F1Fan

Recommended Posts

My main question is what are all the things that could go wrong to cause an error like "whatever is not a function"?

 

I will simplify this significantly. I have a PHP page that includes a JS file, PHP line 68:

<script type='text/javascript' language="JavaScript" src="javascript.js?<?php echo time(); ?>"></script>

On PHP lines 216 and 218 I have buttons that call two different functions that are in that JS file:

<input type="button" value="Run Function One" onclick="functionOne();">
<input type="button" value="Run Function Two" onclick="functionTwo();">

 

Viewing the source in Firefox, I can click on the JS file to view it, and it shows both functions, BUT functionOne() works perfectly, but functionTwo() throws the "functionTwo is not a function" error. Obviously the JS file is being properly included, because functionOne() is working, and it's not being cached because I'm passing the PHP time variable. And I am sure that there are no typos in the function name or anything else like that.

 

Another odd thing is that the function that it cannot find is between two functions that it CAN find! I'm baffled.

 

So what else could it possibly be? I know sometimes syntax errors can cause this, but in my experience, those syntax errors corrupt the whole JS file, not allowing any of its functions to work. Ideas?

Link to comment
Share on other sites

Uhg. I just figured it out. Thanks Mchl for your reply.

 

If anyone is interested, I had an element ID with the same name as the function that wasn't working. I did NOT have a variable name the same, it is a string. This still baffles me why this caused this error, but I changed the element ID to something different and it now works.

 

My only guess is that it has something to do with AJAX. When the page loads, I have an AJAX function populate a lot of fields. While that's happening, I am disabling the button with the same ID as the name of the function, and re-enabling it under certain conditions. The response from the AJAX is PHP-generated JS code that is then evaluated with the JS eval() function. So, I can only assume that the eval() may be creating that string into a variable, or some other JS element that is not a function, thus throwing the error.

Link to comment
Share on other sites

Interesting. A colleague and I attempted to create a simple test page to replicate the error, and try as we might, we could not get it to occur without the AJAX component.

 

It must have something to do with the placement, although we tried moving the element and the JavaScript to various parts of the page and still couldn't get it to happen again. But, obviously we hadn't tried every possibility. In any case, I'll be sure to keep my element IDs different from my function names, etc.

 

Thanks for the reply.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.