Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you force your Javascript event to run first, regardless of the order in which the events were added?

I have Javascript that people are including in their page. In my Javascript I have a version of jQuery (1.8 for sake of easy reference) that is sectioned off into its own namespace, and referenced via a global variable (but not one of the two default vars of "$" or "jQuery"). This allows users to have jQuery in their page and have it not interfere with the stuff I'm doing internally in my functions.

So we have one page that has jQuery already (1.4), and everything works fine, except that the user and my code are both listening to "click" events on elements, and theirs is going first, so on the few events they do that return false, jQuery stops propagation and my event never gets triggered. I need my event to go first. The user is expecting my onClick functionality to still work.

Now I know that jQuery keeps its own order of events internally through the _data() object, and through this it is possible to unbind existing events, bind my event, then rebind the existing events, but that only applies to objects bound through that instance of jQuery. I'd rather not just blindly look for the jQuery object in hopes that the conflict was introduced by a user's own version of jQuery. After all what happens when a user binds the event not through jQuery? Trying to manipulate the existing jQuery object in the page isn't a good solution.

I know that, depending on browser, they are using addEventListener/removeEventListener or attachEvent/detachEvent. If only I could get a listing of the already added events, I could rebind them in the order I wanted, but I can't find out how. Looking through the DOM via Chrome inspect I don't see onclick bound anywhere (not on the object, not on window or document either).

I'm having the darndest time trying to figure out just exactly where jQuery binds its listening. To be able to control the order of its own events, jQuery must blanketly listen somewhere and then fire off its own functions right? If I could figure out where that's done I might get some insight into how to ensure my event is always first. Or maybe there's some Javascript API I haven't been able to find on Google.

Any suggestions?

like image 838
Scott Avatar asked Dec 20 '12 20:12

Scott


People also ask

How do you prioritize events in JavaScript?

JavaScript events don't take any priorities. When and event is fired it is added to an event queue and executed as soon as possible. You can claim that it is a general rule that onclick attribut events will always trigger first, and that will always be true, but it's not because they are prioritized.

What is the first step in adding event handling to your JavaScript code?

What is the first step in adding event handling to your JavaScript code? Using event handlers you can trigger several different functions using a single event. When does a browser event fire? An event listener listens is triggered the first time an event takes place.

How do you handle an event in JavaScript?

JavaScript Event HandlersEvent handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.


Video Answer


1 Answers

We solved this by just adding a little jQuery extension that inserts events at the head of the event chain:

$.fn.bindFirst = function(name, fn) {   var elem, handlers, i, _len;   this.bind(name, fn);   for (i = 0, _len = this.length; i < _len; i++) {     elem = this[i];     handlers = jQuery._data(elem).events[name.split('.')[0]];     handlers.unshift(handlers.pop());   } }; 

Then, to bind your event:

$(".foo").bindFirst("click", function() { /* Your handler */ }); 

Easy peasy!

like image 198
Chris Heald Avatar answered Oct 07 '22 21:10

Chris Heald