9

I have a JavaScript file, Mybasefile.js, which has the function Mybasefunction(). I want to override this function in another JavaScript file. When the function is called in a button click, I want the original Mybasefunction() to be executed along with some other code. How can I do this?

1 Answer 1

20

place this code in the override file. Make sure the override file is included after the orginal one.

var orig_Mybasefunction = window.Mybasefunction;
window.Mybasefunction = function(){
    orig_Mybasefunction();
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 this should work - @Jis see the edit, it should fix the problem.
You might want to hide the orig_Mybasefunction variable by wrapping this code in an anonymous function: (function () { var orig_ ... }()); That way, you won't clobber the global namespace, and you'll be able to use the name orig_Mybasefunction elsewhere without disturbing this code.
I know, I know -- but let's get the basics down first.

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.