addEventListener - change execution order

Discussion in 'JavaScript' started by Howlett, Jun 15, 2013.

  1. #1
    Hello everyone,

    Let's suppose an element has some attached functions on keydown, which where added using the addEventListener function.

    I want to add my own function to be executed but move its execution to the top. In other words I want my function to be executed before any other.

    Changing the source and adding my own addEventListener first is not an option :/

    Has anyone faced anything a similar issue? Do you have any ideas on how to solve it?

    I would like to mention that ANY solution is acceptable even if it requires additional JS frameworks etc.

    Thanks in advance :)
     
    Howlett, Jun 15, 2013 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    Okay, well I don't know why you can't edit your Javascript, which is really the only way to do it properly.

    The only other way (it might not even work, I'm not a JS expert) is at the very bottom of your html, just before </body> execute code to remove the event listener, then add a new one with your function first.
     
    blueparukia, Jun 16, 2013 IP
  3. Howlett

    Howlett Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I cannot edit the JavaScript because it's another website. I am trying to add extra functionality using a browser extension (the extension actually adds a custom JS file to the page's source and the work is done by the external javascript file).

    My issue with the solution you provided would be that I cannot find a way to list the attached functions to that element.

    I have also tried preventDefault, preventPropagation, inline registration of my function (onkeydown=) but nothing works.

    My problem is that the textarea already has a function bind to it and when the user presses ENTER it gets processed. What I want to do is to add something to the text written BEFORE it gets processed.

    Whatever method I use, I simply cannot get it to execute first. For instance, I simply make it display an alert box and by the time the alert shows up, the text is already processed.
     
    Howlett, Jun 16, 2013 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    Okay, I guess that is a reasonable way to use it.

    My way will really be the only way to do it (though I haven't in practise). If you want to send me a link to the JS file you're trying to edit I'll see if I can knock out an example.
     
    blueparukia, Jun 16, 2013 IP
  5. Howlett

    Howlett Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Well, you can go to facebook.com/messages and pick any conversation you want :p

    That's the textarea I want to hook.
     
    Howlett, Jun 16, 2013 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Couldn't have chosen a worse site to try. I'm having the same problem then, I set onkeydown to null and then overrode it but same issue. The problem with the code in facebook is that I'm sure there are a dozen other handlers such as onsubmit, onkeyup or whatever that's overriding it.
    Or something more stupidly easy. So sorry, but I can't help.
     
    blueparukia, Jun 16, 2013 IP
  7. Howlett

    Howlett Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Well that's my main issue, the complexity of it. Thank you for trying though :)
     
    Howlett, Jun 16, 2013 IP