freaking out!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    freaking out!

    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.
  • chunk1978
    New Member
    • Jan 2007
    • 224

    #2
    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?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      the js runtime system can't decide which one to be called....

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by chunk1978
          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.

          Comment

          • chunk1978
            New Member
            • Jan 2007
            • 224

            #6
            Originally posted by acoder
            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.valu e="4.00" and "textfield.valu e="4.50", etc. (there are approximately 300 of these, and each are written on 2 lines... = 600 lines just for that)

            Comment

            • d3vkit
              New Member
              • Nov 2006
              • 34

              #7
              Originally posted by chunk1978
              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.valu e="4.00" and "textfield.valu e="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?
              Code:
              function check_text_field(el,value) {
                   document.getElementById(el).value = value;
                   return false;
              }
              And I think you could use an array to change two fields, as you did previously...

              Code:
              /*here is the function with arrays passed in, just make sure the elements corrospond to the values! Each el gets a value!*/
              function check_text_field(elArr,valuesArr) {
                   //a for loop through each el passed
                   for(var i=0 ; i<elArr.length; i++ ) {
                        document.getElementById(elArr[i]).value = valuesArr[i];
                   }
                   return false;
              }
              
              //some form element
              <input id="text1" type="text" onchange="return check_text_field('text2','text3','4.00',4.50');" />
              
              <input id="text2" type="text">
              <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
              Code:
              "return function1();"
              
              function function1() {
              do something
              function2();
              return false;
              }
              
              function function2() {
              do something
              function2();
              return false;
              }
              and so on
              And if I am way off on the functions, everyone else please be kind :D

              Comment

              • d3vkit
                New Member
                • Nov 2006
                • 34

                #8
                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.j s 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

                Comment

                • chunk1978
                  New Member
                  • Jan 2007
                  • 224

                  #9
                  thanks a lot for your advice d3vkit... i really do appreciate it

                  Comment

                  • d3vkit
                    New Member
                    • Nov 2006
                    • 34

                    #10
                    Originally posted by chunk1978
                    thanks a lot for your advice d3vkit... i really do appreciate it
                    No problem I love to help whenever I can.

                    Comment

                    Working...