javascript - In IE8: Create a custom event in vanilla JS and pick it up in Jquery -
all of happening ie8.
due script import orders, i'm having bit of code beingness executed before jquery loaded need fire custom event.
this event picked later in bit of code when i'm sure jquery have been loaded. i'd utilize jquery pick event.
i saw asked question: how trigger custom javascript event in ie8? , applied answer, worked, when i'm trying pick event via jquery, nil happens.
here's i've tried:
class="snippet-code-js lang-js prettyprint-override">function event() {} event.listen = function(eventname, callback) { if (document.addeventlistener) { document.addeventlistener(eventname, callback, false); } else { document.documentelement.attachevent('onpropertychange', function(e) { if (e.propertyname == eventname) { callback(); } }); } }; event.trigger = function(eventname) { if (document.createevent) { var event = document.createevent('event'); event.initevent(eventname, true, true); document.dispatchevent(event); } else { document.documentelement[eventname] ++; } }; event.listen('myevent', function() { document.getelementbyid('mydiv-jquery').innertext = "myevent jquery"; }); $(document).on('myevent', function() { document.getelementbyid('mydiv-vanilla').innertext = "myevent vanilla"; }); event.trigger('myevent');
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mydiv-jquery">nothing</div> <div id="mydiv-vanilla">nothing</div>
ps: snippet doesn't seem work in ie. here's jsfiddle should working.
there few problems code.
you shadow built-in window.event
without checking if exists; cause problems other scripts.
you don't preserve this
binding when calling callback onpropertychange
listener. should apply callback document rather calling straight behavior close possible addeventlistener
.
you effort increment document.documentelement[eventname]
while undefined. first phone call alter value nan
, onpropertychange
should pick up, on subsequent calls remain nan
.
you create no effort have .on()
recognize event.listen
function, naturally code in event.listen
never executed listener attached .on()
.
have tried using andrea giammarchi's customevent shim?
javascript jquery internet-explorer-8
No comments:
Post a Comment