Adding events to DOM Elements in javascript by For IE, Mozilla, Opera browsers

To add events to DOM element Dynamically using javascript needs to write different code for different browsers. Browsers Like Mozilla, Opera support the "W3C DOM Level 2 event binding mechanism"  which uses a function on DOM elements called addEventListener.


 Following the way to  add events to DOM element Dynamically for all browser.


if (window.addEventListener != null)
{ // Method for browsers that support addEventListener, e.g. Firefox, Opera, Safari
window.addEventListener("focus", FocusFunction, true);
window.addEventListener("blur", FocusLostFunction, true);
}
else
{ // e.g. Internet Explorer (also would work on Opera)
window.attachEvent("onfocus", FocusFunction);
document.attachEvent("onfocusout", FocusLostFunction); //focusout only works on document in IE
}  


Hope this will help you guys........

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Disable backspace key using Jquery

Devexpress Datebox date formatting in angular 6 with example