bmsande
asked on
Override Javascript function
I have a javascript file that contains a function, "doSubmitTab" which is called throughout my application.
However, on one page, I would like to override the function to simply alert the user...something like "Action is Denied".
What is the javascript syntax to override the the "doSubmitTab" function?
However, on one page, I would like to override the function to simply alert the user...something like "Action is Denied".
What is the javascript syntax to override the the "doSubmitTab" function?
ASKER
So just declare a function of the same name, or is there a special override syntax?
Nope, same name.
If you have this wrapped in a javascript class or something with .prototype declarations, it's a different matter.
But if you have a simple function declaration as show above, then just duplicate the function name. Again, just make sure it ends up rendering underneath the original.
If you have this wrapped in a javascript class or something with .prototype declarations, it's a different matter.
But if you have a simple function declaration as show above, then just duplicate the function name. Again, just make sure it ends up rendering underneath the original.
ASKER
sorry the code above didnt display on my mobile, i'll give that a shot.
No this is not the solution.
As you have written new function, it is always get called, even for earlier calls also.
so better way if you can update existing method then update it like this:
Hope helps you.
As you have written new function, it is always get called, even for earlier calls also.
so better way if you can update existing method then update it like this:
$(function(){
callme('a');
callme();
});
function callme(testVariable)
{
if(typeof(testVariable) == "undefined")
{
alert('without parameter method called');
}
else
{
alert('with parameter method called');
}
}
Hope helps you.
I think that was the point... on one specific page he wants to override all calls of the function.
So on one page, declare the function again and it will override all calls.
So on one page, declare the function again and it will override all calls.
ASKER
So as my luck would have it, the .js file which contains the function I want to override is declared towards the end of the page. The part of the page I have control over is above the declaration...
Is it possible to override?
Is it possible to override?
Hi,
you just need to check page structure. I mean whether you are using master page or user control, etc.
Please share page structure.
And this is correct if that page, from which you have to call same function for different behavior and same page do not contains old function definition then you can write same function definition on that same page with same function name.
In this case it will call function which is declared on the same page.
Please share your comments.
you just need to check page structure. I mean whether you are using master page or user control, etc.
Please share page structure.
And this is correct if that page, from which you have to call same function for different behavior and same page do not contains old function definition then you can write same function definition on that same page with same function name.
In this case it will call function which is declared on the same page.
Please share your comments.
ASKER
I have no idea what page structure, master page, or user control is.
All I know is that I created a very simple function of the same name as the function I want to override and it had no effect. The original function is still called. I assumed that since the .js file which contained the original function was declared AFTER my override function, the order of operation was incorrect.
All I know is that I created a very simple function of the same name as the function I want to override and it had no effect. The original function is still called. I assumed that since the .js file which contained the original function was declared AFTER my override function, the order of operation was incorrect.
Correct. The .js file with the original function MUST be before your override.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This worked beautifully!!!! Thank you!!!!
ASKER
I used the microsoft jquery and plugged in my function into the code provided and it worked on first try!
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
JavaScript functions override the same declaration.
Open in new window