DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: javascript error in IE only

  1. #1
    Join Date
    Mar 2007
    Posts
    2

    javascript error in IE only

    Hi, I am getting an 'object expected' error with the following javascript code, and cannot for the life of me figure out what is causing the problem. Admittedly I don't really know what I'm doing all that well, but the error only seems to occur in IE - it does not crop up in Firefox (any version), Opera, or Safari. Any help as to what I am doing wrong will be greatly appreciated.

    Also, I am trying to make an asynchronous call, and refresh only when things change on the server side, however as you can see from the code below I am not really achieving this in a respectable let alone clever manner - any sugestions about how to clean up or optimize the javascript below would also be greatly appreciated - IE related problems aside.

    The error message seems to be raised by the 'window.onload' function at the very bottom at least that is the line that the error refers to.

    Oh, and the code is supposed to dynamically update a table containing a list of items - again it works fine in every browser other than IE, in which case the table loads but the XMLHttpRequest seems to break down for some reason.

    Code:
    function stripe(id, userID, subset) {
    
      // the flag we'll use to keep track of
      // whether the current row is odd or even
      var even = false;
    
      // if arguments are provided to specify the colours
      // of the even & odd rows, then use the them;
      // otherwise use the following defaults:
      //var evenColor = arguments[1] ? arguments[1] : "#FFF";
      var evenColor = "#FFF";
      //var oddColor = arguments[2] ? arguments[2] : "#EEE";
      var oddColor = "#EEE";
    
      // obtain a reference to the desired table
      // if no such table exists, abort
      var table = document.getElementById(id);
      if (! table) { return; }
    
      // tables can have more than one tbody
      // element, so we'll have to get the list of children
      var tbodies = table.getElementsByTagName("tbody");
      var ques_arr = new Array();
      ques_arr = arguments[2];
      var myElem = document.getElementById('checkup');
      myElem.class = ques_arr[0] + 1;
    
      // and iterate through them...
      for (var h = 0; h < tbodies.length; h++) {
    
        // find all the &lt;tr&gt; elements...
        var trs = tbodies[h].getElementsByTagName("tr");
    
        // ... and iterate through them
        for (var i = 0; i < trs.length; i++) {
    
          // avoid rows that have a class attribute
          // or backgroundColor style
          //if (! hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
    
            // get all the cells in this row...
            var tds = trs[i].getElementsByTagName("td");
    
            // and iterate through them...
            for (var j = 0; j < tds.length; j++) {
    
              var mytd = tds[j];
    
              //mytd.firstChild.nodeValue = mytd.firstChild.nodeValue.replace(/\:.*$/, '');
              if(j == 0){
                mytd.firstChild.nodeValue = ques_arr[1] + i;
              }
              if(j == 1){
                mytd.firstChild.nodeValue = ques_arr[i+2];
              }
    
              if(i == ques_arr[0] - 1){
                mytd.className = "selected";
                mytd.style.backgroundColor = "#346DBE";
              }else{
                mytd.className = "";
                mytd.style.backgroundColor = even ? evenColor : oddColor;
              }
    
              if(i < ques_arr[0] - 1){
                mytd.style.textDecoration = 'line-through';
              }
    
              // avoid cells that have a class attribute
              // or backgroundColor style
              //if (! hasClass(mytd) && ! mytd.style.backgroundColor && ! trs[i].style.backgroundColor) {
              //  mytd.style.backgroundColor = even ? evenColor : oddColor;
              //}
            }
          //}
          // flip from odd to even, or vice-versa
          even =  ! even;
        }
      }
    }
    
    function alter(playlist, userID){
      var xhReq = createXMLHttpRequest();
      var checkup = document.getElementById('checkup').class;
      if(!checkup){checkup = 0;}
      xhReq.open("GET", "../QUES/gen_ques.pl?userID=" + userID + '&checkup=' + checkup, true);
      xhReq.onreadystatechange = function(){
          if(xhReq.readyState != 4){
            return;
          } else {
            var serverResponse = xhReq.responseText;
            var ques_arr = serverResponse.split("\n");
            if(ques_arr.length > 3){
              var current_index = ques_arr[0];
              var i = ((current_index - 1 - 5) > 0) ? (current_index - 6) : 1;
              var max = ((current_index + 12 - 1) < ques_arr.length) ? (current_index + 11) : ques_arr.length;
              var sub_index = current_index - i;
              var subset = new Array();
              subset[0] = sub_index;
              subset[1] = i;
              for(i; i<max; i++)
                subset[subset.length] = ques_arr[i];
    
              stripe(playlist, userID, subset);
              return(null);
            } else {
    	  return(null);
            }
          }
      };
      xhReq.send(null);
    
      return(null);
    }
    
    function createXMLHttpRequest(){
      try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){}
      try { return new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {}
      try { return new XMLHttpRequest(); } catch(e) {}
      //return new XMLHttpRequest();
      alert("XMLHttpRequest not supported");
    
      return null;
    }
    
    
    function run(playlist, userID) {
      alter(playlist, userID);
      setInterval("alter(playlist, userID)", 5000);
    }
    
    window.onload = function(){alter('playlist', 'callerID'); setInterval("alter('playlist', 'callerID');", 5000);}
    Last edited by kettle; 03-20-2007 at 11:03 PM.

  2. #2
    Join Date
    Mar 2007
    Posts
    2

    problem solved

    So this was a really, really simple, stupid error on my part. The
    Code:
    ...
    function alter(playlist, userID){
      var xhReq = createXMLHttpRequest();
      var checkup = document.getElementById('checkup').class;  ...
    This should be:
    Code:
    ...
    function alter(playlist, userID){
      var xhReq = createXMLHttpRequest();
      var checkup = document.getElementById('checkup').className;  ...
    Since this was a problem with my code not properly adhering to the javascript spec, I guess the real question isn't 'Why doesn't this work in IE?', but rather 'Why DOES this work in Firefox, Opera and Safari?'

Similar Threads

  1. Replies: 0
    Last Post: 03-10-2007, 11:24 PM
  2. Replies: 5
    Last Post: 05-06-2006, 10:22 PM
  3. problems with javascript
    By Michael in forum ASP.NET
    Replies: 10
    Last Post: 11-08-2005, 01:19 PM
  4. JavaScript function within XSL
    By Igor in forum XML
    Replies: 1
    Last Post: 01-08-2002, 03:48 PM
  5. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center