By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

freaking out!

chunk1978
100+
P: 224
hello...

i've written a long long javascript form code... 3000+ lines... i'm just nearing the end, and everything has been going fine (after many bump, scratches, and yes even tears)...

today i FINALLY made something work after about 4 or 5 days of trying new things... but suddenly, other stuff that isn't even related to the new function, that was working, that i didn't change at all no longer works!

can someone please give me an explanation why this is happening? is it because there is too much code for the browser to handle? could it be because i'm including 5 or 6 or sometimes 7 functions on some onChange event handlers?

thanks.
Feb 13 '07 #1
Share this Question
Share on Google+
9 Replies

chunk1978
100+
P: 224
nevermind... i realized the problem...

wow... i really wish there was a way to delete these embarrassing posts... *blush*

---

i suppose to make this post useful... i'd like to ask if it is bad practice to include more than one javascript function with event handlers... for example:

onChange="ONE(); TWO(); THREE; FOUR();"

the reason why my code was not working earlier, is because i my code was written so that at the end of (for example) "function TWO();" there was a FIVE(); trigger written within the function, so javascript would travel to "function FIVE();" and execute whatever was written... however, i also had a FIVE(); trigger written at the end of "function THREE()"...so this onChange handeler was actually executing function FIVE(); twice...

but is that really a problem? why does javascript seem to stop when it encounters duplicate functions? is it a security feature or something?

up until now i've been writing javascript like i create flash movies, with the image of a "script playhead" parsing the code one line at a time, or one function at a time... but now i'm thinking the code is all parsed at once... it's the only logical reason i can think of as to why it would stop when it encounters duplicate functions...

any ideas?
Feb 13 '07 #2

dmjpro
100+
P: 2,476
the js runtime system can't decide which one to be called....
Feb 13 '07 #3

acoder
Expert Mod 15k+
P: 16,027
3000 lines of javascript code for a form? Are you sure it is all necessary? I'm sure you can cut that down. Besides validation and onchange event handlers, etc. what else do you have that makes the code so long?
Feb 13 '07 #4

acoder
Expert Mod 15k+
P: 16,027
i suppose to make this post useful... i'd like to ask if it is bad practice to include more than one javascript function with event handlers... for example:

onChange="ONE(); TWO(); THREE; FOUR();"

the reason why my code was not working earlier, is because i my code was written so that at the end of (for example) "function TWO();" there was a FIVE(); trigger written within the function, so javascript would travel to "function FIVE();" and execute whatever was written... however, i also had a FIVE(); trigger written at the end of "function THREE()"...so this onChange handeler was actually executing function FIVE(); twice...

but is that really a problem? why does javascript seem to stop when it encounters duplicate functions? is it a security feature or something?

up until now i've been writing javascript like i create flash movies, with the image of a "script playhead" parsing the code one line at a time, or one function at a time... but now i'm thinking the code is all parsed at once... it's the only logical reason i can think of as to why it would stop when it encounters duplicate functions...

any ideas?
AFAIK, it shouldn't be a problem unless you have an infinite loop or something similar where calling one function affects values in another. In any case, if you do happen to end up in that situation, it usually means that you need to make changes, but sometimes it may be necessary, e.g. to update total fields, though you should have a uniform approach for this to make debugging easier.
Feb 13 '07 #5

chunk1978
100+
P: 224
3000 lines of javascript code for a form? Are you sure it is all necessary? I'm sure you can cut that down. Besides validation and onchange event handlers, etc. what else do you have that makes the code so long?
i'm sure it's not all necessary... hah... but it seems to be working fine... what can i say? it's my first javascript project... although a lot of it are functions that state "if this is selected" than "textfield.value="4.00" and "textfield.value="4.50", etc. (there are approximately 300 of these, and each are written on 2 lines... = 600 lines just for that)
Feb 13 '07 #6

P: 35
i'm sure it's not all necessary... hah... but it seems to be working fine... what can i say? it's my first javascript project... although a lot of it are functions that state "if this is selected" than "textfield.value="4.00" and "textfield.value="4.50", etc. (there are approximately 300 of these, and each are written on 2 lines... = 600 lines just for that)
I don't know exactly what your code is like, but could you write the functions like this, using arguments passed in?
Expand|Select|Wrap|Line Numbers
  1. function check_text_field(el,value) {
  2.      document.getElementById(el).value = value;
  3.      return false;
  4. }
And I think you could use an array to change two fields, as you did previously...

Expand|Select|Wrap|Line Numbers
  1. /*here is the function with arrays passed in, just make sure the elements corrospond to the values! Each el gets a value!*/
  2. function check_text_field(elArr,valuesArr) {
  3.      //a for loop through each el passed
  4.      for(var i=0 ; i<elArr.length; i++ ) {
  5.           document.getElementById(elArr[i]).value = valuesArr[i];
  6.      }
  7.      return false;
  8. }
  9.  
  10. //some form element
  11. <input id="text1" type="text" onchange="return check_text_field('text2','text3','4.00',4.50');" />
  12.  
  13. <input id="text2" type="text">
  14. <input id="text2" type="text">
I haven't tested this and I'm by no means a JS expert, so if anything give you trouble let us know; I may not be able to help but I bet someone else can.

Also, instead of writing function1(); function2(); function3();
why not simply
Expand|Select|Wrap|Line Numbers
  1. "return function1();"
  2.  
  3. function function1() {
  4. do something
  5. function2();
  6. return false;
  7. }
  8.  
  9. function function2() {
  10. do something
  11. function2();
  12. return false;
  13. }
and so on
And if I am way off on the functions, everyone else please be kind :D
Feb 15 '07 #7

P: 35
I apparently can no longer edit my other post, so here is a bit more I wanted to add:
Since this is your first JS project, here's some tips that help me:
I write each area of my sites js in seperate pages, ie validate_form.js has form validation code, login.js has login code. Begin each script with a large commented out header identifying it. I then simply put it all in one script and upload it. This makes editing much easier; when changing an area of a script open that particular script up, fix it, and then paste the whole thing over the area in the main script. You could also create a php script to make a js file with all the other scripts included, or maybe there is one on the net already? If not I'm making it. Even better, though, is when doing it this way you can have each script loaded in, and it makes pinpointing the area of the problem easier.
Also, if you haven't got it already, check out Firebug 1.0 for Firefox. Makes JS Scripting TONS easier... at least in Fx. IE is still a pain :P
Feb 15 '07 #8

chunk1978
100+
P: 224
thanks a lot for your advice d3vkit... i really do appreciate it
Feb 15 '07 #9

P: 35
thanks a lot for your advice d3vkit... i really do appreciate it
No problem I love to help whenever I can.
Feb 16 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.