Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat section of code. 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
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:
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.
 
The problem is that xmlhttp is defined in your preajax function and is local to that function only, so the rest of the getlinks function does not know what xmlhttp is. The solution to this is the following:
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();
  }
  [!]return xmlhttp[/!]
}

function getLinks(){
  [!]var xmlhttp = [/!]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;
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top