Search code examples
javascriptinternet-explorer-8type-mismatchattachevent

addEvent type mismatch IE8


I am getting a TYPE: Mismatch error in IE8 with the following code.

    function showTabs() {
    for (var i = 0; i < tabs.length; i++) {
        tabs[i].style.display = "inline-block";
        if (tabs[i]) {
            console.log(tabs[i] + " " + i);
        }
    }
}

function showThumbBoxes() {
    for (var i = 0; i < thumbsContainers.length; i++) {
        thumbsContainers[i].style.display = "block";
        if (thumbsContainers[i]) {
            console.log(thumbsContainers[i] + " " + i);
        }
    }
}

function loadImages() {
    for (var i = 0; i < lazyImages.length; i++) {
        if (lazyImages[i].getAttribute("data-src")) {
            lazyImages[i].src = lazyImages[i].getAttribute("data-src");
            if (lazyImages[i]) {
                console.log(lazyImages[i] + " " + i);
            }
        }
    }
}

function hideContainers() {
    for (var i = 0; i < hiddenContainers.length; i++) {
        hiddenContainers[i].style.display = "none";
        if (hiddenContainers[i]) {
            console.log(hiddenContainers[i] + " " + i);
        }
    }
}

function setUpPage() {
    showTabs();
    showThumbBoxes();
    loadImages();
    hideContainers();
}

if (window.addEventListener) {
    window.addEventListener("load", setUpPage())
} else {
    window.attachEvent("load", setUpPage()); <<< Here seems to be causing issues.
}

I have steppped through the code and it goes through everything correctly and everything gets loaded to the page. After I step through the last curly brace of setUpPage function, it is back on the attachEvent("load", setUpPage()); When I click step through again, I get the mismatch error. Not sure what is going on but because of the error the rest of my script will not load.

Anyone have an idea?


Solution

  • With attachEvent you need to add on + name of the event, so the event will be called onload

    UPDATE

    Also the second parameter of both of the event listeners, are callbacks, so they get executed when the event is triggered. To be able to achieve that, you need to remove the parenthesis of the function call.