0

I'm working on an application where I'm using JS to inject some elements into the DOM. I have a javascript function that I can't change the code for as it's part of a 3rd party install I don't have access to - it also changes the HTML content of the page when called. I can access it For simplicity's sake, I'll omit the detailed code and substitute with example code below.

cant_change() is fired during an event I don't have access to.

function I can't change

function cant_change() {
  return "can't change";
}

function I can change

function will_change() {
  return "will change";
}

The specific question I have is: How can I fire the function will_change after cant_change fires? I can use jQuery too if that helps.

4
  • Be more specific: do you know what event is firing cant_change()? Commented Apr 21, 2015 at 20:18
  • 1
    Is it possible to listen for the event that causes cant_change to be fired? If you can't do that, the only thing I can think to do would be to monkey patch the library implementing cant_change so that it calls the original implementation before yours. If cant_change is also async this could be pretty tricky. Commented Apr 21, 2015 at 20:19
  • 1
    Oh yeah, and the monkey patch suggestion also assumes that cant_change is actually a public method rather than just a function definition. If that's the case you won't be able to call it or patch it in directly at all and will have to go by the event. Commented Apr 21, 2015 at 20:21
  • That’s what I was thinking… @OverlappingElvis Commented Apr 21, 2015 at 20:21

2 Answers 2

2

If function cant_change doesn't have any kind of event that you can listen to, and you can replace it with another function, you could do a workaround and encapsulate it inside another function and call both cant_change and will_change:

function encapsulated() {
  cant_change();
  will_change();
}
Sign up to request clarification or add additional context in comments.

Comments

1

The easy answer is that you can't.

However, most well designed third party JS plugins will give you the option to attach functions to various events that they will then call for you. I would suggest you look through the documentation for the plugin you're using and specifically look for events that they trigger, and how to use them.

Hopefully they will have one for what you're trying to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.