8

The code works as it should in Firefox and Chrome.

When the page loads in Internet Explorer, you get the error

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB6.5; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; eSobiSubscriber 2.0.4.16; FDM; InfoPath.3) Timestamp: Wed, 18 Aug 2010 13:48:54 UTC

and when you press a link

<a href="javascript:toggleLayer('sub< ? echo $l; ?>');"><? echo $alp[$l]; ?></a> 

you get the error

Message: Object expected
Line: 4
Char: 1
Code: 0
URI:

Message: Object required
Line: 24
Char: 4
Code: 0
URI:

There are no errors in Firefox or Chrome, just IE.

Any help would be appreciated. Here is my code:

function toggleLayer( whichLayer )
{
  var sub=new Array();
  for (i=1;i<25;i++)
  {
  sub[i] = 'sub'+i;
 }
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  

  
  for (i=1;i<26;i++)
 {
   eelem = document.getElementById( sub[i] );
   vvis = eelem.style;
   if(eelem==elem){
    vvis.display = "block";
    } else {
    vvis.display = "none";
    }
 }
 
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
2
  • 3
    The error your are having is related to your Javascript, not your PHP. I will retag your question.
    – HoLyVieR
    Commented Aug 18, 2010 at 13:57
  • 1
    Oh no, another instance of if (document.all); you're still supporting very old browsers like IE 4 and NN 4? Commented Aug 20, 2010 at 10:11

1 Answer 1

5

The Problem

The "Object expected"/"Object required" error message in Internet Explorer is rather vague, but it's usually triggered when you are trying to access an attribute of an element which is undefined.

In your case, the undefined element you are probably trying to access is most probably located here :

for (i=1;i<26;i++)
 {
   eelem = document.getElementById( sub[i] ); // <---- UNDEFINED
   vvis = eelem.style; // <----- ERROR
   if(eelem==elem){
    vvis.display = "block";
    } else {
    vvis.display = "none";
    }
 }

The element "eelem" is undefined on the last iteration of the array, because you have previously only initialized the array "sub" with the index 1 to 24. In the last iteration your are trying to access the index 25. So in the last iteration here's what happen :

eelem = document.getElementById( sub[i] );

Is the same as

eelem = document.getElementById( undefined );

Which return undefined to eelem. The error is triggered when you are accessing an attribute of the variable eelem, because undefined does not have attribute.

The Solution

To prevent this from happening you should either initialize the array with the index 1 to 25 or in your second loop, only loop the element 1 to 24.

3
  • Beat me to it, but I think the essence of the solution is that your first loop's condition is i < 25, while the second, it's i < 26. Make them both 25, and you'll be good.
    – Ryan Kinal
    Commented Aug 18, 2010 at 14:12
  • Couple other suggestions... Don't bother looping twice (skip the array of IDs entirely) and do check the return value of getElementById() before trying to use it... Commented Aug 18, 2010 at 15:06
  • There's also an error in this line: toggleLayer('sub< ? echo $l; ?>'). If I'm not mistaken, you have to use <? without a space inbetween (or better: don't use short tags, but <?php). Commented Aug 20, 2010 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.