MrFish Posted February 21, 2010 Share Posted February 21, 2010 I'm making custom events in javascript and they won't work in IE. This is the javascript specific for the listeners- function addEventListener(obj, eventType, listener, capturing) { if(obj.addEventListener) { obj.addEventListener(eventType, listener, capturing); } else if(obj.attachEvent) { obj.attachEvent("on" + eventType, listener); } } And here is the full code if you want to try it <html><head> <title>Event Tests</title> <script type="text/javascript"> function addEventListener(obj, eventType, listener, capturing) { if(obj.addEventListener) { obj.addEventListener(eventType, listener, capturing); } else if(obj.attachEvent) { obj.attachEvent("on" + eventType, listener); } } function Events() { var draggable = document.getElementsByClassName("draggable"); for(var x in draggable) { addEventListener(draggable[x], "mousedown", drag, true); addEventListener(draggable[x], "mouseup", drop, false); } } // Listeners function drag() { this.innerHTML += "1"; } function drop() { this.innerHTML = "dropped"; } // function init() { Events(); } </script> <style type="text/css"> div { float: left; margin: 10px; background-color: #0ff; width: 100px; height: 100px; text-align: center; } .draggable { background-color: #ff0; pointer; move; } </style></head><body onload="init()"> <div></div> <div></div> <div class="draggable"></div> <div></div></body></html> Anyone know what's up? Link to comment Share on other sites More sharing options...
boen_robot Posted February 21, 2010 Share Posted February 21, 2010 Try the functions at the bottom of this page. Link to comment Share on other sites More sharing options...
sairfan1 Posted February 23, 2010 Share Posted February 23, 2010 IE do not support getElementsByClassName you can use instead getElementsByTagName, getElementsById and getElementsByName I'm making custom events in javascript and they won't work in IE. This is the javascript specific for the listeners-function addEventListener(obj, eventType, listener, capturing) { if(obj.addEventListener) { obj.addEventListener(eventType, listener, capturing); } else if(obj.attachEvent) { obj.attachEvent("on" + eventType, listener); } } And here is the full code if you want to try it <html><head> <title>Event Tests</title> <script type="text/javascript"> function addEventListener(obj, eventType, listener, capturing) { if(obj.addEventListener) { obj.addEventListener(eventType, listener, capturing); } else if(obj.attachEvent) { obj.attachEvent("on" + eventType, listener); } } function Events() { var draggable = document.getElementsByClassName("draggable"); for(var x in draggable) { addEventListener(draggable[x], "mousedown", drag, true); addEventListener(draggable[x], "mouseup", drop, false); } } // Listeners function drag() { this.innerHTML += "1"; } function drop() { this.innerHTML = "dropped"; } // function init() { Events(); } </script> <style type="text/css"> div { float: left; margin: 10px; background-color: #0ff; width: 100px; height: 100px; text-align: center; } .draggable { background-color: #ff0; pointer; move; } </style></head><body onload="init()"> <div></div> <div></div> <div class="draggable"></div> <div></div></body></html> Anyone know what's up? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.