473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Ajax object conflicting?

Hi,

I am relatively new to Ajax and am wondering if someone can shed a bit of light on the conceptual part of how Ajax handles its communication.

Not knowing a whole lot about Ajax when I started coding I copied some code from the Internet that seem to be the accepted way of creating the object and using it.
This is the code I am using on the Javascript side:
------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp
  2. var vTrgtListObj
  3.  
  4.  
  5. function get_test_diagnosis(vCodeList,pTrgtListObj)
  6. vTrgtListObj = pTrgtListObj
  7.  
  8. xmlHttp=GetXmlHttpObject()
  9. if (xmlHttp==null)
  10.  {
  11.  alert ("Browser does not support HTTP Request")
  12.  return
  13.  } 
  14. var url="test_diagnosis_lookup.php?code_list=" + vCodeList;
  15. url=url+"&sid="+Math.random()
  16. xmlHttp.onreadystatechange=stateChanged
  17. xmlHttp.open("GET",url,true)
  18. xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  19. xmlHttp.send(null)
  20. }
  21.  
  22. function stateChanged() 
  23. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  24.  { 
  25.    // Delete all elements in target list
  26.    var vCurrElmnt = 0
  27.    // document.getElementById("txtHint").innerHTML=xmlHttp
  28.  
  29.    if(xmlHttp.responseText.length > 0) {
  30.      vDiagSrcListObj = document.getElementById('diag_list_available');
  31.      vDiagSrcListObj.length = 0;
  32.      var aResponseData;
  33.  
  34.      aListData = xmlHttp.responseText.split("\n")
  35.      vMaxCount = aListData.length;
  36.      for(vCurrElement = 0; vCurrElement < vMaxCount - 1; vCurrElement++) {
  37.  
  38.          aResponseData = aListData[vCurrElement].split("    ")
  39.          vFields = new Option(aResponseData[1], "",false,false);
  40.          var j = vDiagSrcListObj.length;
  41.          vDiagSrcListObj.options[j] = vFields;
  42.          vDiagSrcListObj.options[j].value = aResponseData[0];
  43.  
  44.  
  45.      }
  46.    }
  47.  
  48.  
  49.  }
  50. }
  51.  
  52. function GetXmlHttpObject()
  53. {
  54. var xmlHttp=null;
  55. try
  56.  {
  57.  // Firefox, Opera 8.0+, Safari
  58.  xmlHttp=new XMLHttpRequest();
  59.  }
  60. catch (e)
  61.  {
  62.  //Internet Explorer
  63.  try
  64.   {
  65.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  66.   }
  67.  catch (e)
  68.   {
  69.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  70.   }
  71.  }
  72. return xmlHttp;
  73. }
  74.  
------------------------------------------------------------------------

This code is stored in a file and included in the HTML page. I have another file that has roughly this same code in it but does something entirely different than this one. The other one was the original Ajax code that I wrote to do a auto-lookups of reference data from an HTML form.

When I included the above file in the HTML page the original code stopped working. It doesn't execute the callback segment of the code. If I comment out the new Javascript code the original lookups start working again.

I have tried using a different variable to store the XMLHttpRequest object in. When I do the code in this file, although none of it is executed, will produce errors stating the new object variable has no properties. I understand if I'm executing both of these files at the same time and am using the same variable to store the XMLHttpRequest object it would produce a conflict. But how does the second Javascript code produce errors when the first Javascript code is running?

I need to implement Ajax functionality for 2 different purposes but in the same HTML page. How do I do this without causing a conflict?

If you have the time, I would appreciate a short description of how the XMLHttpRequest object communicates so I can understand how the objects should be implemented.

Thanks for your time,

Robert Pollard
Sep 24 '07 #1
2 1308
Sorry for being too quick to post but I figured out the problem. I was using the same callback function name in both files. I guess it was calling both callbacks in each file when the callback occurred.

Regards,

Robert
Sep 24 '07 #2
pbmods
5,821 Expert 4TB
Heya, Robert. Welcome to TSDN!

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: bobzimuta | last post by:
I'm creating a simple AJAX library. It's an object that will return an array containing the response text or xml. I'm trying to find a way to assign the response as a property of the object, but...
3
by: Sandman | last post by:
So, I've used ajax for quite some time for different stuff. Mostly I just feed a funktion I made with the ID of the DIV that should be updated with the output from page XXX.php Now I want to...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
5
by: Sommer.pde | last post by:
It took me some time to find out when and why this happens. When the window of Internet Explorer is closed with an AJAX call still pending, something funny happens. Do it twice, and you will have...
9
by: Tim Johnson | last post by:
I'm an experienced web/CGI programmer, but new to ajax. I'd like to review some examples of how to post data from a form to server using AJAX. I have already created some examples successfully...
3
by: gunjan123 | last post by:
Dear Friends , I am new in ajax .Follwoning code give error - var ajaxpack=new Object() ajaxpack.basedomain="http://"+window.location.hostname ajaxpack.ajaxobj=createAjaxObj()...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
9
Claus Mygind
by: Claus Mygind | last post by:
I am having trouble escaping the & in a JSON.stringfy() ajax call. I don't even know if I am stating the problem correctly here. In my app I have linked json2.js from http://www.JSON.org/json2.js...
2
by: Early Woods | last post by:
Hey, In the following code the function showdivs() is called on body load, though only the second div is loaded. I've been searching on the web for similar problems and I know it has to do with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.