F1Fan Posted June 1, 2010 Share Posted June 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203551-not-a-function-error/ Share on other sites More sharing options...
Mchl Posted June 1, 2010 Share Posted June 1, 2010 Can you show functionTwo() definition? Quote Link to comment https://forums.phpfreaks.com/topic/203551-not-a-function-error/#findComment-1066262 Share on other sites More sharing options...
F1Fan Posted June 1, 2010 Author Share Posted June 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203551-not-a-function-error/#findComment-1066277 Share on other sites More sharing options...
Psycho Posted June 1, 2010 Share Posted June 1, 2010 It is not AJAX specific. You just have to be careful about your naming in JS. I run into the same problem every so often when I accidentally give a function the same name as an object ID. Quote Link to comment https://forums.phpfreaks.com/topic/203551-not-a-function-error/#findComment-1066335 Share on other sites More sharing options...
F1Fan Posted June 1, 2010 Author Share Posted June 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203551-not-a-function-error/#findComment-1066340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.