Javascript Overwrite a Function
This isn’t something I’ve particularly needed to do with javascript until now. Usually I write a few lines of code or functions and can easily change them. This problem came to me as the current work system I’m using which is pretty shoddy requires that certain templates are pulled in from a set of shared templates. We avoid editing these templates as it can effect all our websites using them. We can set these templates up specifically for a new site allowing us to edit them etc but then we loose the benefits of fixes etc. So becomes the madness of trying to use jquery and other nonsense to work around the templates and change them on the fly. I know its a backwards way of doing things but hey its out of my control and that’s what I’m stuck with for now. Anyway I have certain problems where one of these shared templates has something like a javascript function to validate a form and now i have a request to do something else to that form too. The problem being I cannot edit or access this shared template i only have access to the outer layout template. Usually in an oop environment you can extend the class and override the function so i tried to do something along the same lines and ended up with this.
function ValidateForm(myFormId)
{
//do stuff to validate form
alert('validated '+myFormId);
}
//keep old function write new and call old too
var oldfunc = ValidateForm;
window.ValidateForm = function(myFormId){
oldfunc.apply(this, arguments);
//do stuff after validate form original function
alert('more stuff');
}
//calling the function, imaging its on a form submit or something
ValidateForm('testform');
Comments
Comments are currently closed