javascript - How to stop event propagation when eventListener attached to same elements in IE 8? -
how can stop executing other event handlers with pure javascript in case if attached same element in ie8?
i can stop event propagation event.stopimmediatepropagation method, it's not supported ie8.
// document.getelementbyid('my-elem').attachevent('click', firsthandler); document.getelementbyid('my-elem').addeventlistener('click', firsthandler); // document.getelementbyid('my-elem').attachevent('click', secondhandler); document.getelementbyid('my-elem').addeventlistener('click', secondhandler); function firsthandler(ev){ ev.stoppropagation(); alert('1'); } function secondhandler(ev){ ev.stoppropagation(); alert('2'); }
<div id="my-elem"> how stop propagation ? </div>
a simplified (and error prone) polyfill/shim. capture original attachevent/detachevent methods, , create new ones check flag.
also prevent events attached before 1 calling stopimmediatepropagation.
if (!event.prototype.stopimmediatepropagation) { (function() { var attachevent = element.prototype.attachevent, detachevent = element.prototype.detachevent; element.prototype.attachevent = function(type, callback) { attachevent.call(this, "on"+type, function(event) { if (!event.immediatepropagationstopped) { callback(event); } }); }; element.prototype.detachevent = function(type, callback) { detachevent.call(this, type, callback); }; }()); event.prototype.stopimmediatepropagation = function() { this.immediatepropagationstopped = true; }; } document.getelementbyid("test").attachevent("click",function(e){ console.log("hi"); }); document.getelementbyid("test").attachevent("click",function(e){ e.stopimmediatepropagation(); console.log("hi2"); }); document.getelementbyid("test").attachevent("click",function(e){ console.log("hi3"); });
<div id="test">click me</div>
while did work in ie 8 emulation mode in ie 11 browser, mentioned earlier simplified. doesn't error checking , other needed checks. find proper polyfill add/removeeventlistener, preventdefault, stoppropagation, stopimmediatepropagation if want production usable code.
modernizr lists es5 dom shim having polyfill stopimmediatepropagation might have polyfills others
Comments
Post a Comment