Skip to content

Instantly share code, notes, and snippets.

@vmattos
Last active May 23, 2018 11:23
Show Gist options
  • Save vmattos/7897274 to your computer and use it in GitHub Desktop.
Save vmattos/7897274 to your computer and use it in GitHub Desktop.
attachEvent polyfill I made for my buddy @leandrooriente, who is nuts about IE. Now you can enjoy your Micro$oft event handlers everywhere! I'm a douchebag.
(function(win){
if(win.attachEvent) return;
var attachEvent = function(on, callback) {
var on = on.substring(2,on.length);
return this.addEventListener(on, callback);
}
win.Element.prototype.attachEvent = attachEvent;
})(window);
<!DOCTYPE html>
<html>
<head>
<title>attachEvent-polyfill Test</title>
</head>
<body>
<a id='btn-click' href="http://www.google.com">Turn me green</a>
<script src="polyfill.js"></script>
<script>
var btnClick = document.querySelector('#btn-click');
btnClick.attachEvent('onclick', function(event){
event.preventDefault();
this.style.color = 'green';
});
</script>
</body>
</html>
@leandrooriente
Copy link

Beautiful, but i cant use querySelector with IE 7 =(

@vmattos
Copy link
Author

vmattos commented Jul 25, 2014

You don't want to use a attachEvent polyfill with IE7 either

@rhartvig
Copy link

if this makes you a douchebag, you're a very dear douchebag to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment