Answered by:
Simple problem in javascript: Uncaught ReferenceError: function_name is not defined.

Question
-
Hello,
I have a little problem in a simple javascript app with cordova and jquery defined:
When I launch app on Javascript console I read that:
Uncaught ReferenceError: prova is not defined.
And that is not true, I defined the function in javascript as follow:
function onDeviceReady() { // Handle the Cordova pause and resume events document.addEventListener( 'pause', onPause.bind( this ), false ); document.addEventListener( 'resume', onResume.bind( this ), false ); // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here. var b = document.getElementById("bottone"); b.addEventListener('click', prova, false); }; function prova() { $("p").hide(); };
And then I have a lot of Uncaught ReferenceError: p is not defined while <p> is simple defined in my html as follow:
<p>Hello, your application is ready!</p>
So my question is:
As this simple application run without problems after build, can I ignore these errors or there is a solution?
Thank you
Monday, December 1, 2014 11:03 AM
Answers
-
Just try reordering your code this way. Defining the prova function before it's referenced:
function prova() { $("p").hide(); } function onDeviceReady() { // Handle the Cordova pause and resume events document.addEventListener( 'pause', onPause.bind( this ), false ); document.addEventListener( 'resume', onResume.bind( this ), false ); // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here. var b = document.getElementById("bottone"); b.addEventListener('click', prova, false); };
- Proposed as answer by Deric Ferreira Monday, December 1, 2014 12:33 PM
- Unproposed as answer by Deric Ferreira Monday, December 1, 2014 12:34 PM
- Marked as answer by Aidoru Monday, December 1, 2014 2:28 PM
Monday, December 1, 2014 11:31 AM
All replies
-
Just try reordering your code this way. Defining the prova function before it's referenced:
function prova() { $("p").hide(); } function onDeviceReady() { // Handle the Cordova pause and resume events document.addEventListener( 'pause', onPause.bind( this ), false ); document.addEventListener( 'resume', onResume.bind( this ), false ); // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here. var b = document.getElementById("bottone"); b.addEventListener('click', prova, false); };
- Proposed as answer by Deric Ferreira Monday, December 1, 2014 12:33 PM
- Unproposed as answer by Deric Ferreira Monday, December 1, 2014 12:34 PM
- Marked as answer by Aidoru Monday, December 1, 2014 2:28 PM
Monday, December 1, 2014 11:31 AM -
Thank you for your answer Deric,
that resolved the problem but still don't understand why these errors:
<p> is simple in html file, so why change function order resolve the problem?
And then, why I don`t have same errors with the "onPause"and "onResume" functions? The situation is like my function prova, they are defined after the "addEventListerner()"
And finally, why app can run if I had these errors?
Thank you
- Edited by Aidoru Monday, December 1, 2014 11:53 AM
Monday, December 1, 2014 11:51 AM -
Aidoru good that your problem is solved. Now we have a question about browser engine and definitions.
Your problem does not occurs in runtime because all functions are already loaded in your window context. I think this is a IDE problem, that does not load the entire context (document, dependences, scripts) in development time.
- Edited by Deric Ferreira Monday, December 1, 2014 12:43 PM
Monday, December 1, 2014 12:43 PM -
Thank you Deric,
javascript debug is so different than other languages debug, not so strict rules in javascript, an error could be a "not error" and app can live with it.
- Edited by Aidoru Monday, December 1, 2014 1:11 PM
Monday, December 1, 2014 1:10 PM -
Yes it is. And what's your conclusion? Was your question answered?
- Edited by Deric Ferreira Monday, December 1, 2014 1:16 PM
- Marked as answer by Aidoru Monday, December 1, 2014 2:22 PM
- Unmarked as answer by Aidoru Monday, December 1, 2014 2:27 PM
Monday, December 1, 2014 1:15 PM -
The problem has been solved, for the answer about how it is possibile to solve it by change functions order I think that it will remain a sort of thing without a logical answer
Thank you
Monday, December 1, 2014 2:27 PM