window.attachEvent()

They have: 426 posts

Joined: Feb 2005

Hi All,

I am having some issues with attaching Events in IE.

I use the below to attach or addEvent depending on the browser:

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

But I am getting errors in IE with the window.attachEvent?

Any ideas the error is: "Object doesn't support this property or method" on the line with the window.attachEvent.

Any ideas?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Can you provide a link to the page where this code is no functioning? The code you show here looks fine.

Or you might consider using a JavaScript framework to abstract away all the IE "features". I use jQuery. All you would need to write is:

$(document).ready(onloadfunction);

He has: 629 posts

Joined: May 2007

Hmm. I'd expect problems in non-IE browsers, as the obj.addEventListener() requires the event type to be spelled without the "on" prefix. Personally, I'd go with window.load = onloadfunction; myself...

@teammatt3 - benf may have a reason to wait until the page is completely loaded--the jQuery "ready" function fires when the DOM is complete, but before all resources have downloaded. A subtle but important difference.

Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.