getElementsByTagName, outerHTML, innerHTML in mozilla...

P

Pai

Hello there,

I have the following peice of javascript which works with IE but would
not work with mozilla.

In IE the first for loop is entered but in mozilla i would not enter
the for loop any tips / ideas?


var tblObj=document.body.getElementsByTagName("TD")

for(var iTblObjLoop=0;iTblObjLoop<tblObj.length;iTblObjLoop++)
{
var sTempStr=new String(tblObj[iTblObjLoop].outerHTML)
alert(sTempStr);
//ms-banner contains the Portal Links
if(sTempStr.indexOf("ms-banner",1)>0)
{
var aDivs = tblObj[iTblObjLoop].getElementsByTagName("A");
for(var i=0;i<aDivs.length - 1;i++)
{
//If hlMySite Exists, change it to 'Admin' Link
if(aDivs.outerHTML.indexOf("hlMySite",1)>0)
{
hlMySite.outerHTML="<a target='_self' id='hlMySite'
href='default.aspx' title='Admin'>Admin</a><BR><BR>"
}
/*
If SettingsOrReturnURL Exists, Append "<BR><BR>" to the Link to
make the rest of the
links to come on the next line
*/
if(aDivs.outerHTML.indexOf("SettingsOrReturnURL",1)>0)
{
SettingsOrReturnURL.outerHTML=SettingsOrReturnURL.outerHTML +
"<BR><BR>"
}
}
break
}
}

Any help would be appreciated,

Thanks,
Srikanth Pai
 
M

Michael Winter

I have the following peice of javascript which works with IE but would
not work with mozilla.

Please, don't use tabs when posting code. Replace them with 2 or 4 spaces.
In IE the first for loop is entered but in mozilla i would not enter
the for loop any tips / ideas?

I should think that it enters the first loop, but errors immediately with
the first statement of that loop - the outerHTML property.
var tblObj=document.body.getElementsByTagName("TD")

for(var iTblObjLoop=0;iTblObjLoop<tblObj.length;iTblObjLoop++)
{
var sTempStr=new String(tblObj[iTblObjLoop].outerHTML)

The outerHTML property is always a string. Why are you creating a string
object?
alert(sTempStr);
//ms-banner contains the Portal Links
if(sTempStr.indexOf("ms-banner",1)>0)

Or more commonly:

if( -1 != sTempStr.indexOf( 'ms-banner' ))
{
var aDivs = tblObj[iTblObjLoop].getElementsByTagName("A");
for(var i=0;i<aDivs.length - 1;i++)
{
//If hlMySite Exists, change it to 'Admin' Link
if(aDivs.outerHTML.indexOf("hlMySite",1)>0)


if( -1 != aDivs[ i ].outerHTML.indexOf( 'hlMySite' ))
{
hlMySite.outerHTML="<a target='_self' id='hlMySite' " +
"href='default.aspx' title='Admin'>Admin</a><BR><BR>"

Is it really necessary to completely replace the link? Wouldn't it be
simpler to modify the appropriate attributes?
}
/*
If SettingsOrReturnURL Exists, Append "<BR><BR>" to the Link
to make the rest of the links to come on the next line
*/
if(aDivs.outerHTML.indexOf("SettingsOrReturnURL",1)>0)
{
SettingsOrReturnURL.outerHTML=SettingsOrReturnURL.outerHTML +
"<BR><BR>"
}
}
break
}
}


To make this appropriate for the Internet, it would require a lot of
changes. For a start, feature detection. However, it seems that this would
best be performed on a server, not a client.

Mike
 
S

Srikanth Pai

Hello Mike,

Thanks for your reply,

It does not enter the first loop it self I am positive, I put an alert
statement right after the for loop and i could not see the popup? any
other suggestions / ideas why it is not getting through?

var tblObj=document.body.getElementsByTagName("TD")
for(var itblobjloop=0;itblobjloop<tblobj.length;itblobjloop++)
{
alert('hello');
var sTempStr=new String(tblObj[iTblObjLoop].outerHTML)

Thanks
Srikanth Pai
 
M

Michael Winter

It does not enter the first loop it self I am positive, I put an alert
statement right after the for loop and i could not see the popup? any
other suggestions / ideas why it is not getting through?

It wouldn't matter anyway; Mozilla doesn't support the outerHTML property
(not many browsers do).

Have you checked the length of the collection held in tblObj to see if any
elements were found? Are you certain of correct capitalisation? The
snippet below doesn't have the same capitalisation as in your previous
post, and there are inconsistencies, too.

Seeing a test page is the only sure way to debug it.
var tblObj=document.body.getElementsByTagName("TD")
for(var itblobjloop=0;itblobjloop<tblobj.length;itblobjloop++)
{
alert('hello');
var sTempStr=new String(tblObj[iTblObjLoop].outerHTML)

Once again, the outerHTML property returns a string in every browser that
supports it (most don't and will return undefined), so creating a string
object is a waste of browser resources.

I apologise for being blunt, but the script you presented should be
scrapped. It relies on proprietary, and DOM, features; none of which are
tested.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top