I have small bug (I suppose) in my Javascript file. It is simple file but it is not working as expected on Firefox 6 (on Chrome it is working fine). Everything is working except submit(), and validate() is working only when called within document.ready, if I try to access these functions directly, I get...
uncaught exception: ReferenceError: (function) is not defined
Here is the JS file .php?i=CMy4WYPF, it is loaded after jQuery lib, there are no other JS files.
Thank you very much for your help.
I have small bug (I suppose) in my Javascript file. It is simple file but it is not working as expected on Firefox 6 (on Chrome it is working fine). Everything is working except submit(), and validate() is working only when called within document.ready, if I try to access these functions directly, I get...
uncaught exception: ReferenceError: (function) is not defined
Here is the JS file http://pastebin./raw.php?i=CMy4WYPF, it is loaded after jQuery lib, there are no other JS files.
Thank you very much for your help.
Share Improve this question asked Jul 22, 2011 at 9:50 Dejan MarjanovićDejan Marjanović 19.4k7 gold badges53 silver badges67 bronze badges2 Answers
Reset to default 3You don't show how or when the code is loaded, so when it is executed is unknown.
The document markup is not provided so it is impossible to tell what may or may not work, or why. Here are some suggestions for improvements, they may or may not make any difference.
The following statements:
> var exception = $('#teligence-exception');
> var form = $('#teligence-ctc form');
are at the top of the script. Do the elements referenced by the IDs exist in the DOM when the statements are executed?
Searching for codes in an array using jQuery's inArray is inefficient in browsers that lack a native Array.prototype.indexOf method (which is quite a few). Far better to have codes as a string with a delimiter and use the ubiquitous String.prototype.indexOf() method, e.g.
var codes = '|201|202|203|';
...
var x = code.indexOf('|' + val + '|');
The submit function is not called in the posted code.
Much of the script appears very inefficient:
> var val = $(this).val();
> $(this).val( val.replace(/[^0-9]/, '') );
can be
var val = this.value;
this.value = val.replace(/D/g, '');
note that in the original code, only the first non-digit character will be replaced. In the suggested code, all non-digit characters will be replaced (courtesy of the g flag).
Also:
validate($(this).attr('name'));
can be:
validate(this.value);
And so on. The idea is to not create unnecessary jQuery objects.
Dont forget to include ,
<script src="jquery-1.xx.x.min.js" type="text/javascript"> </script>
if you are using jquery, xx-x stand for the