Link to home
Start Free TrialLog in
Avatar of x_terminat_or_3
x_terminat_or_3

asked on

Stack overflow error on line 0, internet explorer

Hello all


I have this javascript that is working fine in Firefox, but makes my life misserable on ie. . .

I think the solution would be to use the body onload event handler but I can't access that because that part of the document is generated by a template.  Changing that would change the entire site (1000+ pages) so that is not an option.


Here is the script, including any hacks I tried specifically for internet explorer.


Basically what I want to do is automatically start a function when the body is loaded.  I check if the body is loaded by looking for an element with a specific name, and if it is not there, set a timeout to check again.

      var http=httpObject(),http_busy=false,CREDIT_REMAIN=0,msie;

      function campaign(bid,status,credits,name) {

            this.bid=(typeof(bid)=='undefined'?0:bid);
            this.status=(typeof(status)=='undefined'?'pending':status);
            this.credits=(typeof(credits)=='undefined'?0:credits);
            this.name=(typeof(name)=='undefined'?'':name);
      }

      function httpObject() {

            var out;

            if(navigator.appName=='Microsoft Internet Explorer') {

                  out=new ActiveXObject('Microsoft.XMLHTTP');

            } else {

                  out=new XMLHttpRequest;
            }

            return out;
      }

      function hasAttribute(attribute,obj) {

            var out=false,counter=0;

            if(obj.attributes.length) {

                  for(counter=0; counter<obj.attributes.length; counter++) {

                        if(obj.attributes[counter].name==attribute) {

                              out=true;

                              break;
                        }
                  }
            }

            return out;
      }

      function sendCampaign(obj) {

            var mbID=find_mbID(obj);

      }

      function find_mbID(child) {

            var out=new Array();

            while( (hasAttribute('bid',child)==false) && (child.parentNode)) {

                  child=child.parentNode;
            }

            if(hasAttribute('bid',child)) {

                  out[0]=child.getAttribute('bid');

            }

            if(hasAttribute('cost',child)) {

                  out[1]=child.getAttribute('cost');
            }

            return out;
      }

      function bulkpush_autostart(interval) {

            var test,tmp;

            if( (test=document.getElementById('bulkpush_campaigns'))) {

                  if((tmp=document.getElementsByName('messageText'))) {

                        showCharsleft(tmp[0],160,'charsleft');
                  }

                  bulkpush_start_queue();

            } else {

                  if(navigator.appName=='Microsoft Internet Explorer') {
                        
                        window.setTimeout('bulkpush_autostart()',150);

                  } else {

                        window.setTimeout(bulkpush_autostart,150); //keep on trying. . .

                  }
            }
      }

      function bulkpush_start_queue() {

            if(http_busy==false) {
                  
                  bulkpush_http_request();

            } else {

                  window.setTimeout(bulkpush_start_queue,1000);
            }


      }


      function ajaxAuth() {

            //search the current document for two hidden fields: ekey, and company and return them suitable for url encoding

            var ekey,company,obj,out='';

            if( (obj=document.getElementsByName('ekey')) && (obj.length)) {

                  ekey=obj[0].value;
            }

            if( (obj=document.getElementsByName('company')) && (obj.length)) {

                  company=obj[0].value;
            }

            if(ekey && company) {
      
                  out='&ekey='+ekey+'&company='+escape(company);
            }

            return out;
      }

      function bulkpush_http_request() {

            var request;

            if(http_busy==false) {

                  http_busy=true;

                  http.open('get','/rpc.php?rpc[]=Bulkpush_Campaigns&module=27&rpc[]=Bulkpush_Credits'+ajaxAuth(),true);

                  http.onreadystatechange=function() {

                        if(typeof(http)=='undefined') {

                              return;
                        }

                        var sError,sCredit,credits,campaigns=new Array(),name,status,bid,credits,counter,theCredit;

                        if( (http) && (http.readyState==4) && (http.responseXML)) {

                              if((sError=http.responseXML.getElementsByTagName('error')) && (sError.length)) {

                                    alert('An error occurred.\n\n'+sError[0].childNodes[0].data);

                              } else {

                                    if((sCredits=http.responseXML.getElementsByTagName('campaign')) && (sCredits.length)) {

                                          for(counter=0; counter<sCredits.length; counter++) {

                                                bid=(hasAttribute('bid',sCredits[counter])?sCredits[counter].getAttribute('bid'):0);
                                                status=(hasAttribute('status',sCredits[counter])?sCredits[counter].getAttribute('status'):'');
                                                credits=(hasAttribute('credits',sCredits[counter])?sCredits[counter].getAttribute('credits'):0);
                                                name=(hasAttribute('name',sCredits[counter])?sCredits[counter].getAttribute('name'):'');

                                                campaigns[campaigns.length]=new campaign(bid,status,credits,name);
                                          }
                                    }

                                    if((sCredits=http.responseXML.getElementsByTagName('credits')) && (sCredits.length)) {

                                          if(hasAttribute('value',sCredits[0])) {

                                                theCredit=sCredits[0].getAttribute('value');
                                          }
                                    }

                                    updateCanvas(campaigns,theCredit);
                              }

                              http_busy=false;

                              bulkpush_start_queue();
                        }
                  }

                  http.send(null);
            }
      }

      function updateCanvas(cData,credit) {
            CREDIT_REMAIN=credit;

            var obj,tCredit,txtCredit,tData;

            if((obj=document.getElementById('creditsleft'))) {

                  clearChildren(obj);

                  tCredit=document.createTextNode(credit);
                  txtCredit=document.createTextNode(' Credit'+(credit==1?'':'s'));

                  if(credit==0) {

                        span=document.createElement('span');
                        span.style.fontWeight='bold';
                        span.style.color='red';

                        span.appendChild(tCredit);

                        obj.appendChild(span);
                        obj.appendChild(txtCredit);

                  } else {
                        obj.appendChild(tCredit);
                        obj.appendChild(txtCredit);
                  }

            }

            if(obj=document.getElementById('bulkpush_campaigns')) {

                  var obj2=obj.getElementsByTagName('tbody');

                  if(obj2 && obj2.length) {

                        obj2=obj2[0];

                        rows=obj2.getElementsByTagName('tr');
                  }
            }

            for(var counter=0; counter<cData.length; counter++) {

                  for(rowcounter=0; rowcounter<rows.length; rowcounter++) {

                        if(rows[rowcounter].lastChild.getAttribute('bid')==cData[counter].bid) {

                              updateRow(rows[rowcounter],cData[counter]);

                        } else {

                              insertRow(obj2,cData[counter]);
                        }
                  }
            }

            bulkpush_start_queue();
      }

      function updateRow(tr,rowData) {

            tr.lastChild.setAttribute('cost',rowData.credits);

            if(rowData.status=='sent') {

                  tr.firstChild.nextSibling.removeChild(tr.firstChild.nextSibling.firstChild);
                  tr.firstChild.nextSibling.appendChild(document.createTextNode('sent'));
                  tr.firstChild.nextSibling.className='status sent';
                  tr.className='strike';
                  tr.lastChild.style.textDecoration='none';

                  removeLastLink(tr.lastChild);

                  /*try {
                        tr.lastChild.removeChild(tr.lastChild.getElementsByTagName('a').lastChild);
                  }
                  catch(e) {
                        ;
                  }*/
            }
      }

      function removeLastLink(td) {

            var obj,counter;

            if( (obj=td.getElementsByTagName('a')) && (obj.length)) {

                  for(counter=0; counter<obj.length; counter++) {

                        if(obj[counter].className=='sender') {

                              td.removeChild(obj[counter]);

                              break;
                        }
                  }
            }
      }

      function insertRow(tbody,rowData) {


      }

      function sendLink(targetObject) {

            var a,img;

            a=document.createElement('a');
            a.href="javascript:alert('oops')";
            a.onclick=function() {
                  return sendCampaign(this);
            }
            a.title="Send now";
            a.className="sender";

            img=document.createElement('img');
            img.src="/images/send.gif";
            img.setAttribute('border',0);
            img.alt="Send Bulk Push";

            a.appendChild(img);
            targetObject.appendChild(a);

      }

      function editLink(targetObject,bid) {

            var a,img;

            a=document.createElement('a');
            a.href="/client/index.php?module=27&amp;bid="+bid+"&amp;baction=edit&amp;page=1";
            a.title="Edit";

            img=document.createElement('img');
            img.src="/images/edit.png";
            img.border="0";
            img.alt="Edit";

            a.appendChild(img);
            targetObject.appendChild(a);

      }

      function deleteLink(targetObject,bid) {

            var a,img;

            a=document.createElement('a');
            a.href="/client/index.php?module=27&amp;bid="+bid+"&amp;baction=delete&amp;page=3";
            a.title="Edit";

            img=document.createElement('img');
            img.src="/images/edit.png";
            img.border="0";
            img.alt="Edit";

            a.appendChild(img);
            targetObject.appendChild(a);
      }

      function clearChildren(pObj) {
            var removeme;

            while((removeme=pObj.firstChild)) {

                  pObj.removeChild(removeme);
            }
      }

      function getModule() {

            var tmp=location.search,out;

            tmp=tmp.match(/module=(\d*)/);

            if(tmp.length) {

                  out=parseInt(tmp[1]);
            }

            return out;
      }

      function sendCampaign(thisObj) {

            var cData=find_mbID(thisObj),obj,tmp;

            if(CREDIT_REMAIN==0) {

                  //try to load remaining credits from div

                  if( (obj=document.getElementById('creditsleft'))) {

                        tmp=obj.childNodes[0].data;

                        tmp=tmp.match(/\d*/);

                        if(tmp.length>1) {

                              CREDIT_REMAIN=tmp[1];
                        }
                  }
            }
            if(cData[0]) {

                  if(CREDIT_REMAIN < parseInt(cData[1])) {

                        alert('Sorry, but you do not have enough credits to send this campaign.\n\n'+
                                'This campaign requires '+cData[1]+' credit'+(cData[1]==1?'':'s')+' and only '+
                                CREDIT_REMAIN+' credit'+(CREDIT_REMAIN==1?'':'s')+' are available.\n\n\n'+
                                'You can purchase more credits by contacting the sales department at sales@txtnation.com\n\n'+
                                'or by using the shopping cart at the top of the page.');
                  } else {

                        try {
                              window.open('https://secure.txtnation.com/client/modules/bulkpush/sendbulkpush.popup.htm?bid='+cData[0]+
                                                '&module='+getModule()+ajaxAuth(),
                                                'sendbulkpush',
                                                'height=350,width=800,toolbar=no, location=no, status=no, menubar=no, scrollbars=no');
                        }

                        catch(e) {

                              alert('A popup-blocker prevented opening of the Send popup.  Please disable it.');
                        }
                  }
            } else {

                  alert('Could not find data for this campaign. . .');
            }

            return false;
      }

      function showCharsleft(cThis,maxLength,displayObjectName) {

            var obj,remain=0;

            try {
                  remain=maxLength-cThis.value.length;
            } catch(e) {
                  ;
            }

            remain=(remain<0?0:remain);

            if((obj=document.getElementsByName(displayObjectName)) && (obj.length)) {

                  obj[0].value=remain;
            }

      }

      if(navigator.appName=='Microsoft Internet Explorer') {

            var b,tmp;

            if((b=document.getElementsByTagName('body')) && (b.length)) {
                  if( (tmp=document.getElementsByName('messageText')) && (tmp.length)) {

                        showCharsleft(tmp[0],160,'charsleft');
                  }

                  bulkpush_start_queue();
            }
      } else {
            bulkpush_autostart();
      }

NOTE: this code is copyright txtNation
ASKER CERTIFIED SOLUTION
Avatar of dakyd
dakyd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of x_terminat_or_3
x_terminat_or_3

ASKER

Hi there


If this window.onload thingy works at least on msie, then the problem is solved.

As for how the functions are called, they are called by the snippet at the bottom of the file:

if(navigator.appName=='Microsoft Internet Explorer') {

          var b,tmp;

          if((b=document.getElementsByTagName('body')) && (b.length)) {
               if( (tmp=document.getElementsByName('messageText')) && (tmp.length)) {

                    showCharsleft(tmp[0],160,'charsleft');
               }

               bulkpush_start_queue();
          }
     } else {
          bulkpush_autostart();
     }

Which gets executed as soon as the javascript has been loaded by the browser.

I'll get back on you on monday, when I'm actually in the office to test this.


Have a good sunday until then.


Cheers


x_terminat_or_3
D'oh, my eyes are playing tricks on me ... I must've missed a closing brace, because I thought that was part of the showCharsLeft() function.

window.onload should be fine on MSIE, it's a standard property.  Just remember that you're setting the onload handler to be a function *object*, so you don't need the parentheses.
Thanks for your contribution.  It was part of the problem but not all of it.  IE is still giving me a hard time but at least it's not stack overflowing anymore. . .


Cheers


x_terminat_or_3