Last active
May 31, 2025 20:31
-
-
Save Integralist/1363964 to your computer and use it in GitHub Desktop.
Attempt at polyfilling addEventListener in IE7 via .htc hack #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Element = function(){}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <PUBLIC:COMPONENT> | |
| <PUBLIC:METHOD NAME="addEventListener" INTERNALNAME="_addEventListener" /> | |
| <script type="text/javascript"> | |
| var element = new Element; | |
| _addEventListener = element.addEventListener; | |
| </script> | |
| </PUBLIC:COMPONENT> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html dir="ltr" lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>addEventListener Shim</title> | |
| <!--[if lte IE 7]> | |
| <script src="IE7.js"></script> | |
| <style type="text/css"> | |
| * { behavior: url(ie_fix.htc); } | |
| </style> | |
| <![endif]--> | |
| </head> | |
| <body> | |
| <div id="testelement" class="testclass"> | |
| This is my test element | |
| </div> | |
| <script type="text/javascript"> | |
| // IE8 (provides access to its 'Element' Interface) | |
| if (window.Element && !window.addEventListener) { | |
| window.Element.prototype.addEventListener = function(type, listener, useCapture) { | |
| this.attachEvent('on' + type, listener); | |
| } | |
| window.Element.prototype.removeEventListener = function(type, listener, useCapture) { | |
| this.detachEvent('on' + type, listener); | |
| } | |
| } | |
| function test() { | |
| alert('I\'m the listener for the addEventListener'); | |
| testelem.removeEventListener('click', test, false); | |
| } | |
| var testelem = document.getElementById('testelement'); | |
| testelem.addEventListener('click', test, false); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment