Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code design: init ajax functions in a seperate file

    Hi!
    Is it normal to put the ajax init functions in a seperate file?

    Most of the post I've read on different forums have all the functions in one javascript file?

    Is there a drawback by putting the init files in one file and the auto suggest in another?
    Isn't it easier to reuse in that way? Does anyone have a link to a tutorial about good design in ajax javascript files?

    Please give me some hints about how to better organize my code!!

    initAjax.js

    Code:
    var isIE;
    var completeTable;
    var completeField;
    var req;
    var resposeXML;
    var index = 0;
    
    function init() {
      completeField = document.getElementById("completeCityField");
      completeTable = document.getElementById("completeCityTable");
    
      if(navigator.appName == "Microsoft Internet Explorer") {
        isIE = "true";
      }
    }
    
    function initRequest(url) { 
      if (window.XMLHttpRequest) { 
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function clearTable() {
      if (completeTable) {
        completeTable.style.visible = false;
        for (loop = completeTable.childNodes.length -1; loop >= 0 ; loop--) {
          completeTable.removeChild(completeTable.childNodes[loop]);
        }
      }
    }

    autoSuggestCity.js

    Code:
    function parseCities(){
      var row;
      var cell;
    
      clearTable();
      completeTable.style.display = "block";
      ....
    }
    
    function doCityRequest(){
      var url = "someUrl";
      ....
    }

    autoSuggestCountry.js

    Code:
    function parseCountry(){
      var row;
      var cell;
       ....
    }
    
    function doCountryRequest(){
      var url = "someUrl";
      ....
    }

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    My 2 cents: keep them separated as you've already done. The only benefit I could think of with doing it the other way is saving the client browser a few http connections to fetch the other files. In my book, code re-usability trumps this transfer overhead.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •