jquery - $(window).on('resize') in JavaScript -
in javascript, following code:
window.onresize = function() { // something. }
the same as:
$(window).on('resize', function () { // something. });
are 2 code blocks above equal, functionality-wise? there advantage or disadvantage (however minor) using 1 or other?
what about:
window.addeventlistener('resize', function(event) { // something. });
they aren't same, in first example, you're affecting event dom object onresize
handler.
the jquery version doing different behind scene. without looking source code, doing:
window.addeventlistener('resize', function () {...})
that said, jquery version , native addeventlistener
still different because jquery adding magic event handler.
and addeventlistenener
prefered way add event dom object, because can add multiple events dom attribute on[event]
you're limited 1 event.
here's bit more it: https://developer.mozilla.org/en-us/docs/web/api/eventtarget/addeventlistener
while you're @ it, read addeventlistener
friend: removeeventlistener
.
Comments
Post a Comment