By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

Cannot create XMLHTTP instance in IE6?

P: 3
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man.

Hi guys, I frequently visit this site to get answers to my problems and this one is really getting to me...

I have a page that allows you to Browse Authors. There are three drop down boxes that auto-populate via AJAX. I have a file which it [javascript] calls and returns the dynamically built XML file in the boxes Nationality, Ethnicity, and Race.

My problem? It works in FireFox, it works in IE7, but not in IE6; it returns the error "Cannot create XMLHTTP instance."

I would link you to the page http://www.alexanderstreet6.com/wols...x?byLetter=all
but i'm not sure if you'll be able to see it (passworded since it's in QA)

Here is my code:

HTML CODE:
Expand|Select|Wrap|Line Numbers
  1. <body onload="javascript: AutoLoadDropDowns();">
  2. ...
  3. <script type="text/javascript">
  4.     function AutoLoadDropDowns()
  5.     {
  6.         var qs = window.location.toString().substring(window.location.toString().indexOf("?"));
  7.  
  8.         var ele1 = document.getElementById('selNationality');
  9.         var ele2 = document.getElementById('selEthnicity');
  10.         var ele3 = document.getElementById('selRace');
  11.  
  12.         if (ele1 != null)
  13.             do_xml('xml/xml.generate.nationalities.aspx', 'selNationality', 'nationalities', qs);
  14.  
  15.         if (ele2 != null)
  16.             do_xml('xml/xml.generate.ethnicities.aspx', 'selEthnicity', 'ethnicities', qs);
  17.  
  18.         if (ele3 != null)
  19.             do_xml('xml/xml.generate.races.aspx', 'selRace', 'races', qs);
  20.     }
  21.     </script>
  22. ...
  23. <select class="inputSubmit" id="selNationality" onchange="javascript:changevalue(this);" name="selNationality" style="width:150px;">
  24.                 <option value="<%=ChangeValue(Request.RawUrl.ToString(), "nationality", "", 1)%>">author nationality</option>
  25.             </select>
  26.             <select class="inputSubmit" id="selEthnicity" onchange="javascript:changevalue(this);" name="selEthnicity" style="width:150px;">
  27.                 <option value="<%=ChangeValue(Request.RawUrl.ToString(), "ethnicity", "", 1)%>">author ethnicity</option>
  28.             </select>
  29.             <select class="inputSubmit" id="selRace" onchange="javascript:changevalue(this);" name="selRace" style="width:150px;">
  30.                 <option value="<%=ChangeValue(Request.RawUrl.ToString(), "race", "", 1)%>">author race</option>
  31.             </select>
  32.  
AJAX SCRIPT
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var notWhitespace = /\S/
  3.  
  4. function cleanWhitespace(node)
  5. {
  6.     for (var x = 0; x < node.childNodes.length; x++)
  7.     {
  8.         var childNode = node.childNodes[x]
  9.         if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue)))
  10.         {
  11.             // that is, if it's a whitespace text node
  12.             node.removeChild(node.childNodes[x])
  13.             x--
  14.         }
  15.         if (childNode.nodeType == 1)
  16.         {
  17.             // elements can have text child nodes of their own
  18.             cleanWhitespace(childNode)
  19.         }
  20.     }
  21. }
  22.  
  23.  
  24.  
  25. var http_request1 = null;
  26. var http_request2 = null;
  27. var http_request3 = null;
  28.  
  29. function GetXmlHttpObject()
  30. {
  31.     var xmlHttp = null;
  32.     if (window.XMLHttpRequest)
  33.     {
  34.         xmlHttp = new XMLHttpRequest(); // Mozilla, Safari,...
  35.         if (xmlHttp.overrideMimeType)
  36.         {
  37.             xmlHttp.overrideMimeType('text/xml');
  38.         }
  39.         else if (window.ActiveXObject)
  40.         {
  41.             try
  42.             {
  43.                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // IE
  44.             }
  45.             catch (e)
  46.             {
  47.                 try
  48.                 {
  49.                     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE
  50.                 }
  51.                 catch (e)
  52.                 {
  53.                     // do nothing
  54.                 }
  55.             }
  56.         }
  57.         return xmlHttp;
  58.     }
  59. }
  60.  
  61. function makeRequest(url, objectname, rootname, qs)
  62. {
  63.     if (rootname == 'nationalities')
  64.     {
  65.         http_request1 = GetXmlHttpObject();
  66.         if (http_request1 == null)
  67.         {
  68.             alert('Cannot create XMLHTTP instance');
  69.             return false;
  70.         }
  71.         else
  72.         {
  73.             http_request1.onreadystatechange = alertContents1;
  74.             http_request1.open('GET', url + qs, true);
  75.             http_request1.send(null);
  76.         }
  77.     }
  78.     if (rootname == 'ethnicities')
  79.     {
  80.         http_request2 = GetXmlHttpObject();
  81.         if (http_request2 == null)
  82.         {
  83.             alert('Cannot create XMLHTTP instance');
  84.             return false;
  85.         }
  86.         else
  87.         {
  88.             http_request2.onreadystatechange = alertContents2;
  89.             http_request2.open('GET', url + qs, true);
  90.             http_request2.send(null);
  91.         }
  92.     }
  93.     if (rootname == 'races')
  94.     {
  95.         http_request3 = GetXmlHttpObject();
  96.         if (http_request3 == null)
  97.         {
  98.             alert('Cannot create XMLHTTP instance');
  99.             return false;
  100.         }
  101.         else
  102.         {
  103.             http_request3.onreadystatechange = alertContents3;
  104.             http_request3.open('GET', url + qs, true);
  105.             http_request3.send(null);
  106.         }
  107.     }
  108. }
  109.  
  110. function alertContents1()
  111. {
  112.     if (http_request1.readyState == 4)
  113.     {
  114.         if (http_request1.status == 200)
  115.         {
  116.             var xmldoc = http_request1.responseXML;
  117.             cleanWhitespace(xmldoc);
  118.  
  119.             var root;
  120.             root = xmldoc.getElementsByTagName('nationalities').item(0);
  121.  
  122.             var arr1 = new Array(root.childNodes.length);
  123.             var arr2 = new Array(root.childNodes.length);
  124.  
  125.             for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
  126.             {
  127.                 var node = root.childNodes[iNode]
  128.                 arr1[iNode] = node.childNodes[0].childNodes[0].nodeValue;
  129.                 arr2[iNode] = node.childNodes[1].childNodes[0].nodeValue;
  130.             }
  131.  
  132.             addrow('selNationality', arr1, arr2);
  133.             if (document.getElementById('selNationality').options.length < 3)
  134.             {
  135.                 document.getElementById('selNationality').style.display = "none";
  136.             }
  137.         }
  138.         else
  139.         {
  140.             alert('There was a problem with the request.');
  141.         }
  142.     }
  143. }
  144.  
  145. function alertContents2()
  146. {
  147.     if (http_request2.readyState == 4)
  148.     {
  149.         if (http_request2.status == 200)
  150.         {
  151.             var xmldoc = http_request2.responseXML;
  152.             cleanWhitespace(xmldoc);
  153.  
  154.             var root;
  155.             root = xmldoc.getElementsByTagName('ethnicities').item(0);
  156.  
  157.             var arr1 = new Array(root.childNodes.length);
  158.             var arr2 = new Array(root.childNodes.length);
  159.  
  160.             for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
  161.             {
  162.                 var node = root.childNodes[iNode]
  163.                 arr1[iNode] = node.childNodes[0].childNodes[0].nodeValue;
  164.                 arr2[iNode] = node.childNodes[1].childNodes[0].nodeValue;
  165.             }
  166.  
  167.             addrow('selEthnicity', arr1, arr2);
  168.             if (document.getElementById('selEthnicity').options.length < 3)
  169.             {
  170.                 document.getElementById('selEthnicity').style.display = "none";
  171.             }
  172.         }
  173.         else
  174.         {
  175.             alert('There was a problem with the request.');
  176.         }
  177.     }
  178. }
  179.  
  180. function alertContents3()
  181. {
  182.     if (http_request3.readyState == 4)
  183.     {
  184.         if (http_request3.status == 200)
  185.         {
  186.             var xmldoc = http_request3.responseXML;
  187.             cleanWhitespace(xmldoc);
  188.  
  189.             var root;
  190.             root = xmldoc.getElementsByTagName('races').item(0);
  191.  
  192.             var arr1 = new Array(root.childNodes.length);
  193.             var arr2 = new Array(root.childNodes.length);
  194.  
  195.             for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
  196.             {
  197.                 var node = root.childNodes[iNode]
  198.                 arr1[iNode] = node.childNodes[0].childNodes[0].nodeValue;
  199.                 arr2[iNode] = node.childNodes[1].childNodes[0].nodeValue;
  200.             }
  201.  
  202.             addrow('selRace', arr1, arr2);
  203.             if (document.getElementById('selRace').options.length < 3)
  204.             {
  205.                 document.getElementById('selRace').style.display = "none";
  206.             }
  207.         }
  208.         else
  209.         {
  210.             alert('There was a problem with the request.');
  211.         }
  212.     }
  213. }
  214.  
  215. function changevalue(ele)
  216. {
  217.     window.location = ele.options[ele.selectedIndex].value;
  218. }
  219.  
  220. function do_xml(scriptname, objectname, rootname, qs)
  221. {
  222.     if (rootname == "nationalities")
  223.     {
  224.         document.getElementById('selNationality').onclick = "";
  225.     }
  226.  
  227.     if (rootname == "ethnicities")
  228.     {
  229.         document.getElementById('selEthnicity').onclick = "";
  230.     }
  231.  
  232.     if (rootname == "races")
  233.     {
  234.         document.getElementById('selRace').onclick = "";
  235.     }
  236.  
  237.     makeRequest(scriptname, objectname, rootname, qs);
  238. }
  239.  
  240. function addrow(objectname, arr1, arr2)
  241. {
  242.     var dropdown = document.getElementById(objectname);
  243.  
  244.     for (r = 0; r < arr1.length; r++)
  245.     {
  246.         var txt = arr1[r];
  247.         var val = arr2[r];
  248.         var newOpt;
  249.  
  250.         if (objectname == "selNationality")
  251.         {
  252.             if (Url.decode(getQueryVariable("nationality")).replace( /\+/g, " " ) == txt)
  253.             {
  254.                 newOpt = new Option(txt, val, true, true);
  255.             }else{
  256.                 newOpt = new Option(txt, val, false, false);
  257.             }
  258.         }
  259.  
  260.         if (objectname == "selEthnicity")
  261.         {
  262.             if (Url.decode(getQueryVariable("ethnicity")).replace( /\+/g, " " ) == txt)
  263.             {
  264.                 newOpt = new Option(txt, val, true, true);
  265.             }else{
  266.                 newOpt = new Option(txt, val, false, false);
  267.             }
  268.         }
  269.  
  270.         if (objectname == "selRace")
  271.         {
  272.             if (Url.decode(getQueryVariable("race")).replace( /\+/g, " " ) == txt)
  273.             {
  274.                 newOpt = new Option(txt, val, true, true);
  275.             }else{
  276.                 newOpt = new Option(txt, val, false, false);
  277.             }
  278.         }
  279.  
  280.         dropdown.options[dropdown.options.length] = newOpt;
  281.     }
  282. }
  283.  
  284. function getQueryVariable(variable) {
  285.     var query = window.location.search.substring(1);
  286.     var vars = query.split("&");
  287.  
  288.     for (var i=0;i<vars.length;i++) {
  289.         var pair = vars[i].split("=");
  290.         if (pair[0] == variable) {
  291.             if (pair[1].toString().trim() == ""){
  292.                 return null;
  293.             }else{
  294.                 return pair[1];
  295.             }
  296.         }
  297.     }
  298. }
  299. String.prototype.trim = function() {
  300.     a = this.replace(/^\s+/, '');
  301.     return a.replace(/\s+$/, '');
  302. };
  303. </script>
  304.  
Any help is so greatly appreciated if you come to the northern virginia area I promise you a cold brew!

cheers!
- John West
Aug 20 '07 #1
Share this Question
Share on Google+
4 Replies


dmjpro
100+
P: 2,476
Welcome to TSDN.
It was no need to post all these codes.
Aneway, you made a silly mistake at the first of your GetXmlHttpObject function.
Look there a condition set first that if(window.XMLHttpRequest) and the whole code is under this condition.
Now for IE it is false so ur XMLHttpObject returns NULL.
Your code shud be like this....

Expand|Select|Wrap|Line Numbers
  1. if(window.XMLHttpRequest)
  2. {
  3. //For Mozilla based browser
  4. }
  5. else if(window.ActiveXObject)
  6. {
  7. //For IE browser
  8. }
  9.  
Now I think your code will work well.
Best of luck.

Kind regards,
Dmjpro.
Aug 21 '07 #2

gits
Expert Mod 5K+
P: 5,282
heya ...

yep ;) ... simply put a } to line 38 ... small fix but great improvement ;) ... IE is not to be blamed that time (not for this problem at least), except of needing an extra-handling ... but the XMLHttpRequest was an invention of MS ... so be a little bit generous with it this time ... i know ... it is hard to be generous with that browsers exceptions all the time ;)

kind regards
Aug 21 '07 #3

P: 3
Alas! theScripts community strikes again FTW!

Thank you so much for your guys' help it was definitely much appreciated :)


Cheers,
` John West
Aug 21 '07 #4

dmjpro
100+
P: 2,476
Alas! theScripts community strikes again FTW!

Thank you so much for your guys' help it was definitely much appreciated :)


Cheers,
` John West

Hey what do u mean by FWT????
Aug 21 '07 #5

Post your reply

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