...

View Full Version : code design: init ajax functions in a seperate file



vator
10-20-2008, 10:58 AM
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

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

function parseCities(){
var row;
var cell;

clearTable();
completeTable.style.display = "block";
....
}

function doCityRequest(){
var url = "someUrl";
....
}

autoSuggestCountry.js

function parseCountry(){
var row;
var cell;
....
}

function doCountryRequest(){
var url = "someUrl";
....
}

tomws
10-20-2008, 02:08 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum