Which is a good approach?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    Which is a good approach?

    Hi. I want to call 2 functions on body load.(JS functions).

    So calling both on body load is good or call one function on body load and call other one at the end of first function?

    i mean;

    Code:
    function my fun()
    {
    
    }
    
    function myfun2()
    {
    
    
    }
    <body onLoad="myfun();myfun2()">
    or

    Code:
    fuction myfun()
    {
    
    
    // Do all craps here.
    
    
    myfun2();
    }
    
    function myfun2()
    {
    
    
    // Do all craps here.
    
    }
    
    <body onLoad="myfun()">
    Which approach is good.

    Is there any advantage and disadvantage in both of these?

    Regards
    Dheeraj Joshi
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    that depends a bit on the 'craps' that should be done ... seen from a practical usage ... you could even use:

    Code:
    function initPage() {
        myFun1();
        myFun2();
    }
    and later on:

    Code:
    <body onload="initPage();">
    which is a bit better since usally the scripts are in external files and it is good practice to have single entry and return points in your code ... but its even a bit of taste of the programmer ... i would use the method shown above ... since i think the methods typically should be self contained and in html there should only be one call ... so that i could have a look at the js-file and see what happens when initPage() is called ... i wouldn't destroy the 'semantics' of myFun1() with the call of the second function and might be you want to call it alone somewhere ... but when you call myFun2() within it then you need params or whatever to avoid the call of myFun2() sometimes ...

    kind regards

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Thanks "GITS"

      That was informative.

      I think your approach to my problem is a good one.

      Regards
      Dheeraj Joshi

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Ok.. Back to actual problem.

        I want to create few text boxes dynamically and also i want to incorporate auto update via AJAX.

        So i need dynamic text box creation function and auto update function both to be called on body load.

        Regards
        Dheeraj Joshi

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          ok ... and what is the problem?

          kind regards

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            Not back to actual problem.

            But back to actual question.

            The reason why i asked that question

            Regards
            Dheeraj Joshi

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              I am starting to implement it.

              If problem arises definitely post question.

              Regards
              Dheeraj Joshi

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                ok :) ... i was just confused ;) ... i would suggest to start a new thread when you got problems with it ...

                kind regards

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Yup.

                  I will start new thread. Hope you guys help me out, if i get problems while coding.

                  Regards
                  Dheeraj Joshi

                  Comment

                  Working...