'parentNode' is null or not an object error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bent27
    New Member
    • Dec 2007
    • 2

    'parentNode' is null or not an object error

    I'm new to javascript...I' m getting 'parentNode' is null or not an object error in the following code

    [CODE=javascript] for (var ci=0;ci<headerL ink.parentNode. childNodes.leng th;ci++) {
    if (headerLink.par entNode.childNo des[ci].tagName && headerLink.pare ntNode.childNod es[ci].tagName.toLowe rCase() == 'span') span = headerLink.pare ntNode.childNod es[ci];
    }[/CODE]

    can anybody tell me what is the error ?
    Last edited by gits; Dec 20 '07, 08:31 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    welcome to TSDN ...

    probably headerLink is not a valid node ... so to be sure we need to see more of your code especially the code where headerLink gets assigned a node-reference ...

    kind regards

    Comment

    • bent27
      New Member
      • Dec 2007
      • 2

      #3
      thank you for your reply gits, I hadn't logged in for quite a while, should I include the enitre script file? anyways, I have included it!!

      [CODE=javascript]//BEGIN
      addEvent(window , "load", sortables_init) ;

      //Common Variables
      var SORT_COLUMN_IND EX;
      var oldRows = new Array();

      //=============== =============== =============== =============== =============== ======
      // This function will find all tables with class sortable and make them sortable
      //=============== =============== =============== =============== =============== ======
      function sortables_init( ) {
      // Find all tables with class sortable and make them sortable
      if (!document.getE lementsByTagNam e) return;
      tbls = document.getEle mentsByTagName( "table");
      var sortable =0;
      for (ti=0;ti<tbls.l ength;ti++)
      {
      thisTbl = tbls[ti];
      if (((' '+thisTbl.class Name+' ').indexOf("sor table") != -1) && (thisTbl.id))
      {
      sortable =1;
      var headerTable = eval("document. all."+thisTbl.i d+"header"); //To get the header table
      ts_makeSortable (headerTable,th isTbl); // Pass the header table and detail table

      }
      }
      // get sortcolumn and sortarrow
      // append sortcolumn to sa to get the column
      if((document.fo rms[0].hidSortArrow.v alue == 'null') || (document.forms[0].hidSortArrow.v alue == "")){
      document.forms[0].hidSortArrow.v alue="up";
      document.forms[0].hidSortColumn. value=0;
      }

      var curValue = document.forms[0].hidSortArrow.v alue;
      if(sortable ==1){
      if (document.all.l ist_table.rows. length >1) {
      ts_resortTable( eval('document. all.sa'+documen t.forms[0].hidSortColumn. value), document.all.li st_table);
      if(curValue == "up"){
      ts_resortTable( eval('document. all.sa'+documen t.forms[0].hidSortColumn. value), document.all.li st_table);
      }
      }
      }

      }
      //=============== =============== =============== =============== =============== ======
      // This function will make the tables sortable
      //=============== =============== =============== =============== =============== ======

      function ts_makeSortable (headerTable, detailTable) {
      if (detailTable.ro ws.length<=1) return;
      if (headerTable.ro ws && headerTable.row s.length > 0)
      {
      var firstRow = headerTable.row s[0];
      }

      if (!firstRow) return;

      // We have a first row: assume it's the header, and make its contents clickable links
      for (var i=0;i<firstRow. cells.length;i+ +)
      {
      var cell = firstRow.cells[i];
      var txt = ts_getInnerText (cell);
      //Set the hyperlink for the cell that contains the heading
      cell.innerHTML = '<h1> <a href="#" id=sa'+i+' onclick="ts_res ortTable(this,d ocument.all.'+d etailTable.id+' );return false;">'+txt+' </a><span class="sortarro w"></span></h1>';
      }
      }
      //=============== =============== =============== =============== =============== ======
      //To get the inner text within a cell
      //=============== =============== =============== =============== =============== ======
      function ts_getInnerText (el){
      if (typeof el == "string") return el;
      if (typeof el == "undefined" ) { return el };
      if (el.innerText) return el.innerText; //Not needed but it is faster
      var str = "";

      var cs = el.childNodes;
      var l = cs.length;
      for (var i = 0; i < l; i++) {
      switch (cs[i].nodeType) {
      case 1: //ELEMENT_NODE
      str += ts_getInnerText (cs[i]);
      break;
      case 3: //TEXT_NODE
      str += cs[i].nodeValue;
      break;
      }
      }
      return str;
      }
      //=============== =============== =============== =============== =============== ======
      //To perform the sorting when clicked on the header links
      //=============== =============== =============== =============== =============== ======
      function ts_resortTable( headerLink,deta ilTable) {
      // get the span
      var span;
      for (var ci=0;ci<headerL ink.parentNode. childNodes.leng th;ci++) {
      if (headerLink.par entNode.childNo des[ci].tagName && headerLink.pare ntNode.childNod es[ci].tagName.toLowe rCase() == 'span') span = headerLink.pare ntNode.childNod es[ci];
      }
      var spantext = ts_getInnerText (span);
      //var tcolumn = headerLink.pare ntNode.parentNo de;

      var column = headerLink.pare ntNode.parentNo de.cellIndex; //Obtain the cell index of the cell whose hyperlink was clicked.
      var table = detailTable;



      document.forms[0].hidSortColumn. value=column;

      // Work out a type for the column
      if (table.rows.len gth <= 1) return;
      var itm = ts_getInnerText (table.rows[1].cells[column]);

      sortfn = ts_sort_caseins ensitive;
      if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
      if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
      if (itm.match(/^\w\w\w\s\d\d[\/-]\d\d[\/-]\d\d\s\d\d\:\d\ d\s\w\w$/)) sortfn = ts_sort_date;
      if (itm.match(/^\w\w\w\s\d\d[\/-]\d\d[\/-]\d\d\s\d\d\:\d\ d\s\w\w\s$/)) sortfn = ts_sort_date;
      if (itm.match(/^[?$]/)) sortfn = ts_sort_currenc y;
      if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric ;

      SORT_COLUMN_IND EX = column;

      if(oldRows.leng th <= 0){
      for (j=1;j<table.ro ws.length;j++)
      { oldRows[j-1] = table.rows[j]; }
      }

      var newRows = new Array();
      for(i=0;i<oldRo ws.length;i++)
      newRows[i] = oldRows[i];

      newRows.sort(so rtfn);
      if (span.getAttrib ute("sortdir") == null)
      span.setAttribu te('sortdir','u p');

      if (span.getAttrib ute("sortdir") == 'up') {
      ARROW = '&nbsp; <img src="img/down_arrow.jpg" align="center" border="0">';
      newRows.reverse ();
      span.setAttribu te('sortdir','d own');

      } else {
      ARROW = '&nbsp; <img src="img/up_arrow.jpg" align="center" border="0">';
      newRows.sort(so rtfn);
      span.setAttribu te('sortdir','u p');
      }

      // ARROW=tcolumn.i nnerHTML;

      document.forms[0].hidSortArrow.v alue=span.getAt tribute("sortdi r");


      // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
      // don't do sortbottom rows
      for (i=0;i<newRows. length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.inde xOf('sortbottom ') == -1))) table.tBodies[0].appendChild(ne wRows[i]);}
      // do sortbottom rows only
      for (i=0;i<newRows. length;i++) { if (newRows[i].className && (newRows[i].className.inde xOf('sortbottom ') != -1)) table.tBodies[0].appendChild(ne wRows[i]);}

      // Delete any other arrows that may be showing
      var allspans = document.getEle mentsByTagName( "span");
      for (var ci=0;ci<allspan s.length;ci++) {
      if (allspans[ci].className == 'sortarrow') {
      if (getParent(alls pans[ci],"table") == getParent(heade rLink,"table")) { // in the same table as us?
      allspans[ci].innerHTML = '&nbsp;';
      }
      }
      }

      // applying different back ground colors to alternate rows
      for (j=1;j<table.ro ws.length;j++)
      {
      if(j%2 == 0){
      table.rows[j].bgColor = "#FEFEF5";
      } else {
      table.rows[j].bgColor = "#BCD2EE";
      }
      }

      span.innerHTML = ARROW;
      }
      //=============== =============== =============== =============== =============== ======
      //To get the parent node
      //=============== =============== =============== =============== =============== ======
      function getParent(el, pTagName) {
      if (el == null) return null;
      else if (el.nodeType == 1 && el.tagName.toLo werCase() == pTagName.toLowe rCase()) // Gecko bug, supposed to be uppercase
      return el;
      else
      return getParent(el.pa rentNode, pTagName);
      }
      //=============== =============== =============== =============== =============== ======
      //To do the sorting based on date
      //=============== =============== =============== =============== =============== ======
      function ts_sort_date(a, b) {
      // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
      aa = ts_getInnerText (a.cells[SORT_COLUMN_IND EX]);
      bb = ts_getInnerText (b.cells[SORT_COLUMN_IND EX]);

      if (aa.length >= 21 ) {
      dt1 = aa.substr(10,2) +aa.substr(4,2) +aa.substr(7,2) +aa.substr(19,2 )+aa.substr(13, 2)+aa.substr(16 ,2) ;
      }else if (aa.length == 10) {
      dt1 = aa.substr(6,4)+ aa.substr(3,2)+ aa.substr(0,2);
      } else {
      yr = aa.substr(6,2);
      if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
      dt1 = yr+aa.substr(3, 2)+aa.substr(0, 2);
      }
      if (bb.length >= 21) {
      dt2 = bb.substr(10,2) +bb.substr(4,2) +bb.substr(7,2) +bb.substr(19,2 )+ bb.substr(13,2) +bb.substr(16,2 );
      }else if (bb.length == 10) {
      dt2 = bb.substr(6,4)+ bb.substr(3,2)+ bb.substr(0,2);
      } else {
      yr = bb.substr(6,2);
      if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
      dt2 = yr+bb.substr(3, 2)+bb.substr(0, 2);
      }

      if (dt1==dt2) return 0;
      if (dt1<dt2 ) return -1;

      return 1;
      }
      //=============== =============== =============== =============== =============== ======
      //To sort based on currency
      //=============== =============== =============== =============== =============== ======
      function ts_sort_currenc y(a,b) {
      aa = ts_getInnerText (a.cells[SORT_COLUMN_IND EX]).replace(/[^0-9.]/g,'');
      bb = ts_getInnerText (b.cells[SORT_COLUMN_IND EX]).replace(/[^0-9.]/g,'');
      return parseFloat(aa) - parseFloat(bb);
      }
      //=============== =============== =============== =============== =============== ======
      //To sort numeric values
      ////=============== =============== =============== =============== =============== ======
      function ts_sort_numeric (a,b) {
      aa = parseFloat(ts_g etInnerText(a.c ells[SORT_COLUMN_IND EX]));
      if (isNaN(aa)) aa = 0;
      bb = parseFloat(ts_g etInnerText(b.c ells[SORT_COLUMN_IND EX]));
      if (isNaN(bb)) bb = 0;
      return aa-bb;
      }
      //=============== =============== =============== =============== =============== ======
      //To sort based on caseinsensitive
      //=============== =============== =============== =============== =============== ======
      function ts_sort_caseins ensitive(a,b) {
      aa = ts_getInnerText (a.cells[SORT_COLUMN_IND EX]).toLowerCase() ;
      bb = ts_getInnerText (b.cells[SORT_COLUMN_IND EX]).toLowerCase() ;
      if (aa==bb) return 0;
      if (aa<bb) return -1;
      return 1;
      }
      //=============== =============== =============== =============== =============== ======
      //To sort on a default column
      //=============== =============== =============== =============== =============== ======
      function ts_sort_default (a,b) {
      aa = ts_getInnerText (a.cells[SORT_COLUMN_IND EX]);
      bb = ts_getInnerText (b.cells[SORT_COLUMN_IND EX]);
      if (aa==bb) return 0;
      if (aa<bb) return -1;
      return 1;
      }
      //=============== =============== =============== =============== =============== ======
      //The function is called when the page is loaded
      //=============== =============== =============== =============== =============== ======
      function addEvent(elm, evType, fn, useCapture)
      // addEvent and removeEvent
      // cross-browser event handling for IE5+, NS6 and Mozilla

      {
      if (elm.addEventLi stener){
      elm.addEventLis tener(evType, fn, useCapture);
      return true;
      } else if (elm.attachEven t){
      var r = elm.attachEvent ("on"+evType , fn);
      return r;
      } else {
      alert("Handler could not be removed");
      }
      }
      //=============== =============== =============== =============== =============== ======
      //The function is called when the page is loaded
      //=============== =============== =============== =============== =============== ======
      function doAlert() {
      alert("ContextP ath is: " + context_path);
      }
      //END[/CODE]
      Last edited by gits; Jan 31 '08, 09:37 PM. Reason: added code tags

      Comment

      Working...