Meek
Meek

Reputation: 3362

addEventListener and attachEvent

I'm using this piece of code:

window.addEventListener("load", function () {
    setTimeout(function () {
        // Hide the address bar
        window.scrollTo(0, 1);
    }, 0);
});

but IE doesn't understand addEventListener- it needs attachEvent which seems to give an error in Chrome. How can I check if this method is supported and then serve the right one?

Upvotes: 0

Views: 279

Answers (1)

VisioN
VisioN

Reputation: 145458

As simple as:

if (window.addEventListener) {
    window.addEventListener("load", myFunc, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", myFunc);
}

Upvotes: 1

Related Questions