SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function causes error in IE - why?

    It's been a while since I've attempted any cross browser scripting, but I cant believe this simple function is throwing an error in IE:

    Code:
    function infoBoxSwitch(id) {
        
        var d = document.getElementById(id);
        
        for (var i = 1; i<=12; i++) {
            if (document.getElementById('infoBox'+i)) {
                document.getElementById('infoBox'+i).style.display='none';            
            }
        }
        
        if (d) {
            d.style.display='block';        
        }
    }
    In the HTML, as you'd expect, I have a bunch of DIV's with ID's along the lines of infoBox1, infoBox2, and so on. Works as intended in FF2.0 with the call infoBoxSwitch('infoBox1') - all the DIV's except the one passed to the function are hidden.

    So what's causing IE to say "Object Expected?"

  2. #2
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when are you calling this function? If you are trying to call it before the dom has fully loaded, then that may be the cause of the problem

  3. #3
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't seem to matter if I call it with onLoad or with window.onload() - I still get object expected errors in IE.

    All I can think is that when an object is hidden, IE decides it's no longer there and doesn't exist.

  4. #4
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I solved the problem by including the script in the page, wrapped in script tags, rather than in a linked file. I've no idea why that should make a difference, but to MicroSoft it obviously does.

    Man, I'm getting tired of all this. Ten years I've been doing this kind of crap and yet, somehow, coding cross browser is as murky as it's ever been.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •