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

Problem with getting javascript to run correctly in firefox

P: 67
Alright this runs just fine in IE but in firefox I get nothing

Expand|Select|Wrap|Line Numbers
  1.             function startup()
  2.             {
  3.                 if (window.ActiveXObject)
  4.                 {
  5.                     xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  6.                 }
  7.                 else if (document.implementation.createDocument)
  8.                 {
  9.                     xmlDoc=document.implementation.createDocument("","",null);
  10.                 }
  11.                 else
  12.                 {
  13.                     alert('Your browser cannot handle this script');
  14.                 }    
  15.                 if (xmlDoc!=null)
  16.                 {
  17.                     xmlDoc.async=false;
  18.                     xmlDoc.load("XML/faq.xml");
  19.                     var x=xmlDoc.getElementsByTagName("QUES");
  20.                     for (i=0;i<x.length;i++)
  21.                     {
  22.                         t = x[i].getElementsByTagName("CAT")[0].childNodes[0].nodeValue;
  23.                         categories.push(t);
  24.                     }
  25.                     var categoryList = categories.unique();
  26.  
  27.                     for (var i=categoryList.length-1; i>=0; --i )
  28.                     {
  29.                         selectCategory.options[i]=new Option(categoryList[i], i, true, false);
  30.                     }
  31.  
  32.                     sizeOfCats = selectCategory.options.length;
  33.                     selectCategory.options[sizeOfCats]=new Option("Select", sizeOfCats, true, true);
  34.  
  35.                 }
  36.             }
  37.  
  38.             Array.prototype.unique = function()
  39.             {
  40.                 var r = new Array();
  41.                 o:for(var i = 0, n = this.length; i < n; i++)
  42.                 {
  43.                     for(var x = 0, y = r.length; x < y; x++)
  44.                     {
  45.                         if(r[x]==this[i])
  46.                         {
  47.                             continue o;
  48.                         }
  49.                     }
  50.                     r[r.length] = this[i];
  51.                 }
  52.                 return r;
  53.             }
  54.  
  55.             function clearSelect()
  56.             {
  57.                 selectQuestion.options.length = 0;
  58.             }
  59.  
  60.             function loadSelectQuestion()
  61.             {
  62.                 questions = [];
  63.                 theAnswer.innerHTML="";
  64.                 var theCategory = selectCategory.options[selectCategory.selectedIndex].text;
  65.                 if(theCategory != "Select")
  66.                 {
  67.                     divSelectQuestion.innerHTML = "<p>Select a Question:</p><select id=\"selectQuestion\" onchange=\"loadAnswer()\"></select><br/><br/>";
  68.                     var x=xmlDoc.getElementsByTagName("QUES");
  69.                     for (i=0;i<x.length;i++)
  70.                     {
  71.                         aCategory = x[i].getElementsByTagName("CAT")[0].childNodes[0].nodeValue;
  72.  
  73.                         if(aCategory == theCategory)
  74.                         {
  75.                             aQuestion = x[i].getElementsByTagName("Q")[0].childNodes[0].nodeValue;
  76.                             questions.push(aQuestion);
  77.                             for (var j=questions.length-1; j>=0; --j )
  78.                             {
  79.                                 selectQuestion.options[j]=new Option(questions[j], j, true, false)
  80.                             }
  81.                         }
  82.                         else
  83.                         {
  84.  
  85.                         }
  86.                     }
  87.  
  88.                 sizeOfQues = selectQuestion.options.length;
  89.                 selectQuestion.options[sizeOfQues]=new Option("Select", sizeOfQues, true, true);
  90.  
  91.                 }
  92.                 else
  93.                 {
  94.                     divSelectQuestion.innerHTML = "";
  95.                 }
  96.             }
  97.  
  98.             function loadAnswer()
  99.             {
  100.                 var theQuestion = selectQuestion.options[selectQuestion.selectedIndex].text;
  101.                 var x=xmlDoc.getElementsByTagName("QUES");
  102.                 for (i=0;i<x.length;i++)
  103.                 {
  104.                     aQuestion = x[i].getElementsByTagName("Q")[0].childNodes[0].nodeValue;
  105.  
  106.                     if(aQuestion == theQuestion)
  107.                     {
  108.                         anAnswer = x[i].getElementsByTagName("A")[0].childNodes[0].nodeValue;
  109.                         theAnswer.innerHTML = "Answer:<br/><br/>" + anAnswer + "<br/><br/>";
  110.                     }
  111.                 }
  112.             }
  113.  
  114.         </script>
  115.  
  116.  
thanks for your time.
Jun 8 '09 #1
Share this Question
Share on Google+
8 Replies


P: 67
After doing some more debugging I tried to tune the code a little

Expand|Select|Wrap|Line Numbers
  1.         <script type="text/javascript">
  2.             var categories = new Array();
  3.             var questions = new Array();
  4.             var xmlDoc=null;
  5.             var firstTime;
  6.             var sizeOfCats;
  7.             var sizeOfQues;
  8.  
  9.             function readFile()
  10.             {
  11.                 if (xmlDoc!=null)
  12.                 {
  13.                     var x=xmlDoc.getElementsByTagName("QUES");
  14.                     for (i=0;i<x.length;i++)
  15.                     {
  16.                         t = x[i].getElementsByTagName("CAT")[0].childNodes[0].nodeValue;
  17.                         categories.push(t);
  18.                     }
  19.                     var categoryList = categories.unique();
  20.  
  21.                     for (var i=categoryList.length-1; i>=0; --i )
  22.                     {
  23.                         selectCategory.options[i]=new Option(categoryList[i], i, true, false);
  24.                     }
  25.  
  26.                     sizeOfCats = selectCategory.options.length;
  27.                     selectCategory.options[sizeOfCats]=new Option("Select", sizeOfCats, true, true);
  28.  
  29.                 }
  30.  
  31.             }
  32.  
  33.             function startup()
  34.             {
  35.                 divSelectCategory.innerHTML = "<select id=\"selectCategory\" onchange=\"loadSelectQuestion()\"></select>";
  36.  
  37.                 if (window.ActiveXObject)
  38.                 {
  39.                     xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  40.                     xmlDoc.async=false;
  41.                     xmlDoc.load("XML/faq.xml");
  42.                     readFile();
  43.                 }
  44.                 else if (document.implementation.createDocument)
  45.                 {                     
  46.                      xmlDoc = document.implementation.createDocument("", "", null);
  47.                       xmlDoc.onload = readFile();
  48.                      xmlDoc.load("XML/faq.xml");
  49.                 }
  50.                 else
  51.                 {
  52.                     alert('Your browser cannot handle this script');
  53.                 }
  54.             }
  55.  
  56.             Array.prototype.unique = function()
  57.             {
  58.                 var r = new Array();
  59.                 o:for(var i = 0, n = this.length; i < n; i++)
  60.                 {
  61.                     for(var x = 0, y = r.length; x < y; x++)
  62.                     {
  63.                         if(r[x]==this[i])
  64.                         {
  65.                             continue o;
  66.                         }
  67.                     }
  68.                     r[r.length] = this[i];
  69.                 }
  70.                 return r;
  71.             }
  72.  
  73.             function clearSelect()
  74.             {
  75.                 selectQuestion.options.length = 0;
  76.             }
  77.  
  78.             function loadSelectQuestion()
  79.             {
  80.                 questions = [];
  81.                 theAnswer.innerHTML="";
  82.                 var theCategory = selectCategory.options[selectCategory.selectedIndex].text;
  83.                 if(theCategory != "Select")
  84.                 {
  85.                     divSelectQuestion.innerHTML = "<p>Select a Question:</p><select id=\"selectQuestion\" onchange=\"loadAnswer()\"></select><br/><br/>";
  86.                     var x=xmlDoc.getElementsByTagName("QUES");
  87.                     for (i=0;i<x.length;i++)
  88.                     {
  89.                         aCategory = x[i].getElementsByTagName("CAT")[0].childNodes[0].nodeValue;
  90.  
  91.                         if(aCategory == theCategory)
  92.                         {
  93.                             aQuestion = x[i].getElementsByTagName("Q")[0].childNodes[0].nodeValue;
  94.                             questions.push(aQuestion);
  95.                             for (var j=questions.length-1; j>=0; --j )
  96.                             {
  97.                                 selectQuestion.options[j]=new Option(questions[j], j, true, false)
  98.                             }
  99.                         }
  100.                         else
  101.                         {
  102.  
  103.                         }
  104.                     }
  105.  
  106.                 sizeOfQues = selectQuestion.options.length;
  107.                 selectQuestion.options[sizeOfQues]=new Option("Select", sizeOfQues, true, true);
  108.  
  109.                 }
  110.                 else
  111.                 {
  112.                     divSelectQuestion.innerHTML = "";
  113.                 }
  114.             }
  115.  
  116.             function loadAnswer()
  117.             {
  118.                 var theQuestion = selectQuestion.options[selectQuestion.selectedIndex].text;
  119.                 var x=xmlDoc.getElementsByTagName("QUES");
  120.                 for (i=0;i<x.length;i++)
  121.                 {
  122.                     aQuestion = x[i].getElementsByTagName("Q")[0].childNodes[0].nodeValue;
  123.  
  124.                     if(aQuestion == theQuestion)
  125.                     {
  126.                         anAnswer = x[i].getElementsByTagName("A")[0].childNodes[0].nodeValue;
  127.                         theAnswer.innerHTML = "Answer:<br/><br/>" + anAnswer + "<br/><br/>";
  128.                     }
  129.                 }
  130.             }
  131.  
  132.         </script>
  133.  
  134.  
Now this works in IE but in firefox it doesn't...
the error the error console gives is

Error: divSelectCategory is not defined
Source File: file:///C:/Users/Administrator/Desktop/CRC/crcFAQ.html
Line: 42

Anyways hope that helps you to help me :D
Jun 9 '09 #2

RamananKalirajan
100+
P: 607
Hi i think the problem may be in the line

Expand|Select|Wrap|Line Numbers
  1.   xmlDoc.onload = readFile(); 
  2.   xmlDoc.load("XML/faq.xml"); 
Can you please check on this.

Regards
Ramanan Kalirajan
Jun 9 '09 #3

acoder
Expert Mod 15k+
P: 16,027
The problem is line 35. divSelectCategory hasn't been defined anywhere.
Jun 9 '09 #4

P: 67
@RamananKalirajan

No that part seems to be working (as far as I can see)
Jun 9 '09 #5

P: 67
@acoder
In my html I have a div with the id divSelectCategory, shouldnt that mean it's defined? (new to javascript sorry)
Jun 9 '09 #6

acoder
Expert Mod 15k+
P: 16,027
No. You need to access it with:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("divSelectCategory")
Jun 9 '09 #7

P: 67
@acoder
oh duh, thanks so much! :D
Jun 9 '09 #8

acoder
Expert Mod 15k+
P: 16,027
No problem. Glad it's working :)
Jun 9 '09 #9

Post your reply

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