I have a section of code that I need to repeat in other functions. To save space and make things cleaner, i tried putting the section that repeats in it's own function, but i'm getting an error and my code isn't executing.
This the entire code:
I have several other functions similar to this and this part is repeated in all of them:
Is there a reason why I can't just put that in a function (called preAjax(); and just put it in each, like this:
_______________
_brian.
This the entire code:
Code:
function getLinks(){
var xmlhttp=false; //Clear our fetching variable
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
xmlhttp = new
ActiveXObject('Microsoft.XMLHTTP');
}
catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
var file = 'blah_inc.php?links';
xmlhttp.open('GET', file, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var content = xmlhttp.responseText;
if( content ){
document.getElementById('links').innerHTML = content;
}
}
}
xmlhttp.send(null)
return;
}
I have several other functions similar to this and this part is repeated in all of them:
Code:
var xmlhttp=false; //Clear our fetching variable
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
xmlhttp = new
ActiveXObject('Microsoft.XMLHTTP');
}
catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
Is there a reason why I can't just put that in a function (called preAjax(); and just put it in each, like this:
Code:
function preAjax(){
var xmlhttp=false; //Clear our fetching variable
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
xmlhttp = new
ActiveXObject('Microsoft.XMLHTTP');
}
catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
}
function getLinks(){
preAjax();
var file = 'blah_inc.php?links';
xmlhttp.open('GET', file, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var content = xmlhttp.responseText;
if( content ){
document.getElementById('links').innerHTML = content;
}
}
}
xmlhttp.send(null)
return;
}
_______________
_brian.