Link to home
Start Free TrialLog in
Avatar of novice_expert
novice_expert

asked on

Override function in javascript

How can I override some function for an object.
xmlHttpReqPost.onerror = function() is defined when the object is created.
I want to give new definition for onerror(),onload() likewise.
Avatar of SysTurn
SysTurn
Flag of Egypt image

you can do
xmlHttpReqPost.onerror = function_name_here;

or you can declare your function while assigning
xmlHttpReqPost.onerror = function(){ alert('An error occured'); };

The followinf is an example for overriding the built in alert function:

<script type="text/javascript">
<!--
    window.alert('This is the default alert');
    function overrided(msg)
    {
        confirm(msg);
    }
    window.alert = overrided;
    window.alert('This is the overrided one');
//-->
</script>

Kind Regards
Bakr
Avatar of novice_expert
novice_expert

ASKER

basically i had overridden a function like onerror like this....now i want to give new definition of onerror i.e override old definition....
just re-assign it in your object, for example:

var obj = new MyClass();
obj.onerror = function_name;
// re-assign it
obj.onerror = function(){ };

Kind Regards
Bakr
it doesn't work....its calling the old definition?
ASKER CERTIFIED SOLUTION
Avatar of SysTurn
SysTurn
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial