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

work in Internet Explorer only ajax code

100+
P: 150
I'm having problem with this code why i don't know works fine in Internet Explorer only that is my code:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. function AJAX(num){
  3.     XMLObject = false;
  4.     if (window.XMLHttpRequest) { // Mozilla, Safari,...
  5.         XMLObject = new XMLHttpRequest();
  6.         if (XMLObject.overrideMimeType) {
  7.             XMLObject.overrideMimeType('text/xml');
  8.         }
  9.     } else if (window.ActiveXObject){ // IE
  10.         try {
  11.             XMLObject = new ActiveXObject("Msxml2.XMLHTTP");
  12.         } catch (e) {
  13.                 try {
  14.                     XMLObject = new ActiveXObject("Microsoft.XMLHTTP");
  15.                 }catch (e) {
  16.  
  17.                 }
  18.             }
  19.         }
  20.     if (!XMLObject) {
  21.         alert('Cannot create XMLHTTP instance');
  22.         return false;
  23.     }
  24.     XMLObject.open("GET","XML.php");
  25.     XMLObject.onreadystatechange=function(){
  26.         if(XMLObject.readyState==4 && XMLObject.status==200){
  27.             var XMLFILE=XMLObject.responseXML;
  28.             makeList(XMLFILE,num);
  29.         }
  30.     }
  31.     XMLObject.send(null);
  32. }
  33. function makeList(XMLData,num){
  34.     var firstCategory=XMLData.documentElement.firstChild;
  35.     var AllMenus=firstCategory.getElementsByTagName("Menu");
  36.     var firstMenu=AllMenus[num];
  37.     for(i=0;i<document.getElementById("Menu").options.length;i++){
  38.         document.getElementById("Menu").options[i]= null;
  39.     }
  40.     for(i=0;i<firstMenu.childNodes.length;i++){
  41.         document.getElementById("Menu").options[i]=new Option(firstMenu.childNodes[i].firstChild.nodeValue);
  42.     }
  43. }
  44. function makeData(what){
  45.     return what.selectedIndex;
  46. }
  47. </script>
Oct 7 '07 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
hi ...

what error do you get, if one? or could you please tell what is not working?

kind regards
Oct 7 '07 #2

100+
P: 150
that is the error i got :
firstCategory.getElementsByTagName is not a function
Oct 7 '07 #3

gits
Expert Mod 5K+
P: 5,390
hi ...

could you post what you get from:

Expand|Select|Wrap|Line Numbers
  1. var firstCategory=XMLData.documentElement.firstChild;
may be there is a whitespace-problem in your xml-document? try to refer the first element another way ... through tagName or something like that.

kind regards
Oct 7 '07 #4

100+
P: 150
That is my HTML page:
[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" type="text/javascript">
function AJAX(num){
XMLObject = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
XMLObject = new XMLHttpRequest();
if (XMLObject.overrideMimeType) {
XMLObject.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject){ // IE
try {
XMLObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLObject = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) {

}
}
}
if (!XMLObject) {
alert('Cannot create XMLHTTP instance');
return false;
}
XMLObject.open("GET","XML.php");
XMLObject.onreadystatechange=function(){
if(XMLObject.readyState==4 && XMLObject.status==200){
var XMLFILE=XMLObject.responseXML;
makeList(XMLFILE,num);
}
}
XMLObject.send(null);
}
function makeList(XMLData,num){
var firstCategory=XMLData.documentElement.firstChild;
var AllMenus=firstCategory.getElementsByTagName("Menu" );
var firstMenu=AllMenus[num];
for(i=0;i<document.getElementById("Menu").options. length;i++){
document.getElementById("Menu").options[i]= null;
}
for(i=0;i<firstMenu.childNodes.length;i++){
document.getElementById("Menu").options[i]=new Option(firstMenu.childNodes[i].firstChild.nodeValue);
}
}
function makeData(what){
return what.selectedIndex;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="p ost" action="">
<select name="Category" id="Category" onchange="AJAX(makeData(this))">
<option>Cameras &amp; Photo</option>
<option>Consumer Electronics</option>
<option>Jewelry &amp; Watches</option>
<option>Real Estate</option>
</select>
<select name="Menu" id="Menu" onchange="">
<option>- Choose -</option>
</select>
</form>
</body>
</html>[/HTML]
and that is my XML File:

[PHP]<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Commercila>
<CamerasAndPhotos>
<Menu type='Bags, Cases &amp; Straps'>
<subMenu>For Camcorders</subMenu>
<subMenu>For Cameras</subMenu>
</Menu>
<Menu type='Binoculars &amp; Telescopes'>
<subMenu>Binoculars &amp; Monoculars</subMenu>
<subMenu>Eyepieces &amp; Telescope Accs.</subMenu>
<subMenu>Telinoculars &amp; Telescopesescopes</subMenu>
</Menu>
<Menu type='Camcorder Accessories'>
<subMenu>Accessories</subMenu>
<subMenu>Batteries &amp; Chargers</subMenu>
<subMenu>Blank Tapes &amp; Memory</subMenu>
<subMenu>Cables, Cords &amp; Connectors</subMenu>
<subMenu>Lenses &amp; Accessories</subMenu>
</Menu>
<Menu type='Camcorders'>
<subMenu>8mm, Hi8, VHS</subMenu>
<subMenu>Digital 8</subMenu>
<subMenu>DVD</subMenu>
<subMenu>HDD</subMenu>
<subMenu>HDV</subMenu>
<subMenu>MicroMV</subMenu>
<subMenu>MiniDV</subMenu>
<subMenu>Other Digital Formats</subMenu>
</Menu>
<Menu type='Digital Camera Accessories'>
<subMenu>Accessories</subMenu>
<subMenu>Batteries &amp; Chargers</subMenu>
<subMenu>Cables, Cords &amp; Connectors</subMenu>
<subMenu>Digital Photo Frames</subMenu>
<subMenu>Memory Cards</subMenu>
<subMenu>Memory Card Adapters &amp; Readers</subMenu>
</Menu>
<Menu type='Digital Cameras'>
<subMenu>Point &amp; Shoot</subMenu>
<subMenu>Digital SLR</subMenu>
<subMenu>Specialty Digital Cameras</subMenu>
<subMenu>Parts &amp; Repair</subMenu>
<subMenu>Other Digital Cameras</subMenu>
</Menu>
<Menu type='Film'>
<subMenu>16MM</subMenu>
<subMenu>35MM</subMenu>
<subMenu>APS</subMenu>
<subMenu>Medium, Large Format</subMenu>
<subMenu>Polaroid &amp; Instant Print</subMenu>
<subMenu>Processing &amp; Other</subMenu>
</Menu>
<Menu type='Film Camera Accessories'>
<subMenu>Cables, Cords &amp; Connectors</subMenu>
<subMenu>Camera Body Accessories</subMenu>
<subMenu>Remotes &amp; Wireless</subMenu>
<subMenu>Other Film &amp; Movie Accessories</subMenu>
</Menu>
<Menu type='Film Cameras'>
<subMenu>35mm Point &amp; Shoot</subMenu>
<subMenu>35mm Rangefinder</subMenu>
<subMenu>35mm SLR</subMenu>
<subMenu>APS</subMenu>
<subMenu>Cameras for Kids</subMenu>
<subMenu>Disposable</subMenu>
<subMenu>Instant Print, Polaroid</subMenu>
<subMenu>Large Format</subMenu>
<subMenu>Medium Format</subMenu>
<subMenu>Movie</subMenu>
<subMenu>Parts &amp; Repair</subMenu>
<subMenu>Submini, Disc, 110</subMenu>
<subMenu>Underwater</subMenu>
<subMenu>Other Film Cameras</subMenu>
</Menu>
<Menu type='Film Processing &amp; Darkroom'>
<subMenu>Chemistry</subMenu>
<subMenu>Enlargement Equip. &amp; Supplies</subMenu>
<subMenu>Finishing Equipment</subMenu>
<subMenu>Hardware, Lighting &amp; Setup</subMenu>
<subMenu>Loupes &amp; Magnifiers</subMenu>
<subMenu>Photographic Paper</subMenu>
<subMenu>Processing Equipment</subMenu>
<subMenu>Other Darkroom Equipment</subMenu>
</Menu>
<Menu type='Flashes &amp; Accessories'>
<subMenu>Digital Camera Flash Units</subMenu>
<subMenu>Film Camera Flash Units</subMenu>
<subMenu>Flash Accessories</subMenu>
</Menu>
<Menu type='Lenses &amp; Filters'>
<subMenu>Digital Camera Lenses</subMenu>
<subMenu>Film Camera Lenses</subMenu>
<subMenu>Filter Accessories</subMenu>
<subMenu>Filters</subMenu>
<subMenu>Lens Accessories</subMenu>
</Menu>
<Menu type='Lighting &amp; Studio Equipment'>
<subMenu>Background Material &amp; Equip</subMenu>
<subMenu>Booms, Stands &amp; Supports</subMenu>
<subMenu>Continuous Lighting</subMenu>
<subMenu>Light Controls &amp; Modifiers</subMenu>
<subMenu>Light Meters</subMenu>
<subMenu>Portable Flash/Strobe</subMenu>
<subMenu>Props &amp; Stage Equipment</subMenu>
<subMenu>Slaves</subMenu>
<subMenu>Studio Flash/Strobe</subMenu>
<subMenu>Other Lighting &amp; Studio Items</subMenu>
</Menu>
<Menu type='Manuals, Guides &amp; Books'>
<subMenu>How-To, Guides &amp; Techniques</subMenu>
<subMenu>Camera Manuals</subMenu>
<subMenu>CheatSheets for Cameras</subMenu>
<subMenu>Lens Manuals</subMenu>
<subMenu>Other Manuals, Guides &amp; Books</subMenu>
</Menu>
<Menu type='Photo Albums &amp; Archive Items'>
<subMenu>Archival &amp; Mounting Materials</subMenu>
<subMenu>Photo Albums</subMenu>
<subMenu>Other Albums &amp; Archive Items</subMenu>
</Menu>
<Menu type='Printers, Scanners &amp; Supplies'>
<subMenu>Photo Printers</subMenu>
<subMenu>Printing Software &amp; Supplies</subMenu>
<subMenu>Scanners</subMenu>
</Menu>
<Menu type='Professional Video Equipment'>
<subMenu>Blank Tapes &amp; Media</subMenu>
<subMenu>Cables &amp; Wires</subMenu>
<subMenu>Cameras</subMenu>
<subMenu>Editing, Post-Production</subMenu>
<subMenu>Manuals</subMenu>
<subMenu>Monitors</subMenu>
<subMenu>Recorders &amp; Players</subMenu>
<subMenu>Video Switchers, Routers</subMenu>
<subMenu>Other</subMenu>
</Menu>
<Menu type='Projection Equipment'>
<subMenu>Accessories</subMenu>
<subMenu>Projectors</subMenu>
<subMenu>Screens</subMenu>
<subMenu>Other Projection Equipment</subMenu>
</Menu>
<Menu type='Stock Photography &amp; Footage'>
</Menu>
<Menu type='Tripods, Monopods'>
<subMenu>Accessories</subMenu>
<subMenu>Monopods</subMenu>
<subMenu>Tripods</subMenu>
<subMenu>Other Tripod, Monopod Items</subMenu>
</Menu>
<Menu type='Vintage'>
<subMenu>Accessories</subMenu>
<subMenu>Cameras</subMenu>
<subMenu>Projectors</subMenu>
<subMenu>Other Vintage Items</subMenu>
</Menu>
<Menu type='Wholesale Lots'>
<subMenu>Cameras</subMenu>
<subMenu>General Accessories</subMenu>
<subMenu>Vintage</subMenu>
<subMenu>Other</subMenu>
</Menu>
</CamerasAndPhotos>
</Commercila>";
[/PHP]

Please try to test my code and see the error
Oct 7 '07 #5

Post your reply

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