Tech Sharing

Tech Sharing

Advertisement

Attaching event in both MSIE and FF with Javascript

By on August 27, 2009

Most of the developer who working in MS platform will be used to the javascript window.attachEvent.


But maybe some of you don’t know that window.attachEvent is only working on IE7+ and Opera only. Your coding will hit an javascrpit error when running at IE 6 or any version of firefox.


To resolve this issue, you may need to modify your code as such:

Original Code:

window.attachEvent(“onload”, Page_load);

function Page_load(){
//function code
}


Modify code:

//set page event handlers
if (window.attachEvent) {

//IE and Opera
window.attachEvent(“onload”, Page_load);

} else if (window.addEventListener) {

// IE 6
window.addEventListener(“load”, Page_Unload, false);

} else {

//FireFox
document.addEventListener(“load”, Page_Unload, false);

}

function Page_load(){

//function code

}


Comments

No Responses to “Attaching event in both MSIE and FF with Javascript”

Write a Comment

Captcha *

%d bloggers like this: