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_bus y=false,CR EDIT_REMAI N=0,msie;
function campaign(bid,status,credit s,name) {
this.bid=(typeof(bid)=='un defined'?0 :bid);
this.status=(typeof(status )=='undefi ned'?'pend ing':statu s);
this.credits=(typeof(credi ts)=='unde fined'?0:c redits);
this.name=(typeof(name)==' undefined' ?'':name);
}
function httpObject() {
var out;
if(navigator.appName=='Mic rosoft Internet Explorer') {
out=new ActiveXObject('Microsoft.X MLHTTP');
} 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.len gth; counter++) {
if(obj.attributes[counter] .name==att ribute) {
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',chil d)) {
out[0]=child.getAttribute( 'bid');
}
if(hasAttribute('cost',chi ld)) {
out[1]=child.getAttribute( 'cost');
}
return out;
}
function bulkpush_autostart(interva l) {
var test,tmp;
if( (test=document.getElementB yId('bulkp ush_campai gns'))) {
if((tmp=document.getElemen tsByName(' messageTex t'))) {
showCharsleft(tmp[0],160,' charsleft' );
}
bulkpush_start_queue();
} else {
if(navigator.appName=='Mic rosoft Internet Explorer') {
window.setTimeout('bulkpus h_autostar t()',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_que ue,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.getElementsB yName('eke y')) && (obj.length)) {
ekey=obj[0].value;
}
if( (obj=document.getElementsB yName('com pany')) && (obj.length)) {
company=obj[0].value;
}
if(ekey && company) {
out='&ekey='+ekey+'&compan y='+escape (company);
}
return out;
}
function bulkpush_http_request() {
var request;
if(http_busy==false) {
http_busy=true;
http.open('get','/rpc.php? rpc[]=Bulk push_Campa igns&modul e=27&rpc[] =Bulkpush_ Credits'+a jaxAuth(), true);
http.onreadystatechange=fu nction() {
if(typeof(http)=='undefine d') {
return;
}
var sError,sCredit,credits,cam paigns=new Array(),name,status,bid,cr edits,coun ter,theCre dit;
if( (http) && (http.readyState==4) && (http.responseXML)) {
if((sError=http.responseXM L.getEleme ntsByTagNa me('error' )) && (sError.length)) {
alert('An error occurred.\n\n'+sError[0].c hildNodes[ 0].data);
} else {
if((sCredits=http.response XML.getEle mentsByTag Name('camp aign')) && (sCredits.length)) {
for(counter=0; counter<sCredits.length; counter++) {
bid=(hasAttribute('bid',sC redits[cou nter])?sCr edits[coun ter].getAt tribute('b id'):0);
status=(hasAttribute('stat us',sCredi ts[counter ])?sCredit s[counter] .getAttrib ute('statu s'):'');
credits=(hasAttribute('cre dits',sCre dits[count er])?sCred its[counte r].getAttr ibute('cre dits'):0);
name=(hasAttribute('name', sCredits[c ounter])?s Credits[co unter].get Attribute( 'name'):'' );
campaigns[campaigns.length ]=new campaign(bid,status,credit s,name);
}
}
if((sCredits=http.response XML.getEle mentsByTag Name('cred its')) && (sCredits.length)) {
if(hasAttribute('value',sC redits[0]) ) {
theCredit=sCredits[0].getA ttribute(' value');
}
}
updateCanvas(campaigns,the Credit);
}
http_busy=false;
bulkpush_start_queue();
}
}
http.send(null);
}
}
function updateCanvas(cData,credit) {
CREDIT_REMAIN=credit;
var obj,tCredit,txtCredit,tDat a;
if((obj=document.getElemen tById('cre ditsleft') )) {
clearChildren(obj);
tCredit=document.createTex tNode(cred it);
txtCredit=document.createT extNode(' Credit'+(credit==1?'':'s') );
if(credit==0) {
span=document.createElemen t('span');
span.style.fontWeight='bol d';
span.style.color='red';
span.appendChild(tCredit);
obj.appendChild(span);
obj.appendChild(txtCredit) ;
} else {
obj.appendChild(tCredit);
obj.appendChild(txtCredit) ;
}
}
if(obj=document.getElement ById('bulk push_campa igns')) {
var obj2=obj.getElementsByTagN ame('tbody ');
if(obj2 && obj2.length) {
obj2=obj2[0];
rows=obj2.getElementsByTag Name('tr') ;
}
}
for(var counter=0; counter<cData.length; counter++) {
for(rowcounter=0; rowcounter<rows.length; rowcounter++) {
if(rows[rowcounter].lastCh ild.getAtt ribute('bi d')==cData [counter]. bid) {
updateRow(rows[rowcounter] ,cData[cou nter]);
} else {
insertRow(obj2,cData[count er]);
}
}
}
bulkpush_start_queue();
}
function updateRow(tr,rowData) {
tr.lastChild.setAttribute( 'cost',row Data.credi ts);
if(rowData.status=='sent') {
tr.firstChild.nextSibling. removeChil d(tr.first Child.next Sibling.fi rstChild);
tr.firstChild.nextSibling. appendChil d(document .createTex tNode('sen t'));
tr.firstChild.nextSibling. className= 'status sent';
tr.className='strike';
tr.lastChild.style.textDec oration='n one';
removeLastLink(tr.lastChil d);
/*try {
tr.lastChild.removeChild(t r.lastChil d.getEleme ntsByTagNa me('a').la stChild);
}
catch(e) {
;
}*/
}
}
function removeLastLink(td) {
var obj,counter;
if( (obj=td.getElementsByTagNa me('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+"& baction=ed it&pag e=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,bi d) {
var a,img;
a=document.createElement(' a');
a.href="/client/index.php? module=27& amp;bid="+ bid+"& baction=de lete&p age=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.first Child)) {
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),o bj,tmp;
if(CREDIT_REMAIN==0) {
//try to load remaining credits from div
if( (obj=document.getElementBy Id('credit sleft'))) {
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()+aja xAuth(),
'sendbulkpush',
'height=350,width=800,tool bar=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,maxLen gth,displa yObjectNam e) {
var obj,remain=0;
try {
remain=maxLength-cThis.val ue.length;
} catch(e) {
;
}
remain=(remain<0?0:remain) ;
if((obj=document.getElemen tsByName(d isplayObje ctName)) && (obj.length)) {
obj[0].value=remain;
}
}
if(navigator.appName=='Mic rosoft Internet Explorer') {
var b,tmp;
if((b=document.getElements ByTagName( 'body')) && (b.length)) {
if( (tmp=document.getElementsB yName('mes sageText') ) && (tmp.length)) {
showCharsleft(tmp[0],160,' charsleft' );
}
bulkpush_start_queue();
}
} else {
bulkpush_autostart();
}
NOTE: this code is copyright txtNation
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_bus
function campaign(bid,status,credit
this.bid=(typeof(bid)=='un
this.status=(typeof(status
this.credits=(typeof(credi
this.name=(typeof(name)=='
}
function httpObject() {
var out;
if(navigator.appName=='Mic
out=new ActiveXObject('Microsoft.X
} 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.len
if(obj.attributes[counter]
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)
child=child.parentNode;
}
if(hasAttribute('bid',chil
out[0]=child.getAttribute(
}
if(hasAttribute('cost',chi
out[1]=child.getAttribute(
}
return out;
}
function bulkpush_autostart(interva
var test,tmp;
if( (test=document.getElementB
if((tmp=document.getElemen
showCharsleft(tmp[0],160,'
}
bulkpush_start_queue();
} else {
if(navigator.appName=='Mic
window.setTimeout('bulkpus
} else {
window.setTimeout(bulkpush
}
}
}
function bulkpush_start_queue() {
if(http_busy==false) {
bulkpush_http_request();
} else {
window.setTimeout(bulkpush
}
}
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.getElementsB
ekey=obj[0].value;
}
if( (obj=document.getElementsB
company=obj[0].value;
}
if(ekey && company) {
out='&ekey='+ekey+'&compan
}
return out;
}
function bulkpush_http_request() {
var request;
if(http_busy==false) {
http_busy=true;
http.open('get','/rpc.php?
http.onreadystatechange=fu
if(typeof(http)=='undefine
return;
}
var sError,sCredit,credits,cam
if( (http) && (http.readyState==4) && (http.responseXML)) {
if((sError=http.responseXM
alert('An error occurred.\n\n'+sError[0].c
} else {
if((sCredits=http.response
for(counter=0; counter<sCredits.length; counter++) {
bid=(hasAttribute('bid',sC
status=(hasAttribute('stat
credits=(hasAttribute('cre
name=(hasAttribute('name',
campaigns[campaigns.length
}
}
if((sCredits=http.response
if(hasAttribute('value',sC
theCredit=sCredits[0].getA
}
}
updateCanvas(campaigns,the
}
http_busy=false;
bulkpush_start_queue();
}
}
http.send(null);
}
}
function updateCanvas(cData,credit)
CREDIT_REMAIN=credit;
var obj,tCredit,txtCredit,tDat
if((obj=document.getElemen
clearChildren(obj);
tCredit=document.createTex
txtCredit=document.createT
if(credit==0) {
span=document.createElemen
span.style.fontWeight='bol
span.style.color='red';
span.appendChild(tCredit);
obj.appendChild(span);
obj.appendChild(txtCredit)
} else {
obj.appendChild(tCredit);
obj.appendChild(txtCredit)
}
}
if(obj=document.getElement
var obj2=obj.getElementsByTagN
if(obj2 && obj2.length) {
obj2=obj2[0];
rows=obj2.getElementsByTag
}
}
for(var counter=0; counter<cData.length; counter++) {
for(rowcounter=0; rowcounter<rows.length; rowcounter++) {
if(rows[rowcounter].lastCh
updateRow(rows[rowcounter]
} else {
insertRow(obj2,cData[count
}
}
}
bulkpush_start_queue();
}
function updateRow(tr,rowData) {
tr.lastChild.setAttribute(
if(rowData.status=='sent')
tr.firstChild.nextSibling.
tr.firstChild.nextSibling.
tr.firstChild.nextSibling.
tr.className='strike';
tr.lastChild.style.textDec
removeLastLink(tr.lastChil
/*try {
tr.lastChild.removeChild(t
}
catch(e) {
;
}*/
}
}
function removeLastLink(td) {
var obj,counter;
if( (obj=td.getElementsByTagNa
for(counter=0; counter<obj.length; counter++) {
if(obj[counter].className=
td.removeChild(obj[counter
break;
}
}
}
}
function insertRow(tbody,rowData) {
}
function sendLink(targetObject) {
var a,img;
a=document.createElement('
a.href="javascript:alert('
a.onclick=function() {
return sendCampaign(this);
}
a.title="Send now";
a.className="sender";
img=document.createElement
img.src="/images/send.gif"
img.setAttribute('border',
img.alt="Send Bulk Push";
a.appendChild(img);
targetObject.appendChild(a
}
function editLink(targetObject,bid)
var a,img;
a=document.createElement('
a.href="/client/index.php?
a.title="Edit";
img=document.createElement
img.src="/images/edit.png"
img.border="0";
img.alt="Edit";
a.appendChild(img);
targetObject.appendChild(a
}
function deleteLink(targetObject,bi
var a,img;
a=document.createElement('
a.href="/client/index.php?
a.title="Edit";
img=document.createElement
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.first
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),o
if(CREDIT_REMAIN==0) {
//try to load remaining credits from div
if( (obj=document.getElementBy
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
CREDIT_REMAIN+' credit'+(CREDIT_REMAIN==1?
'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()+aja
'sendbulkpush',
'height=350,width=800,tool
}
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,maxLen
var obj,remain=0;
try {
remain=maxLength-cThis.val
} catch(e) {
;
}
remain=(remain<0?0:remain)
if((obj=document.getElemen
obj[0].value=remain;
}
}
if(navigator.appName=='Mic
var b,tmp;
if((b=document.getElements
if( (tmp=document.getElementsB
showCharsleft(tmp[0],160,'
}
bulkpush_start_queue();
}
} else {
bulkpush_autostart();
}
NOTE: this code is copyright txtNation
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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
Cheers
x_terminat_or_3
ASKER
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=='Mic
var b,tmp;
if((b=document.getElements
if( (tmp=document.getElementsB
showCharsleft(tmp[0],160,'
}
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