Solved

Override function in javascript

Posted on 2006-07-23
5
3,610 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:novice_expert
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:SysTurn
ID: 17162664
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
0
 
LVL 3

Author Comment

by:novice_expert
ID: 17163422
basically i had overridden a function like onerror like this....now i want to give new definition of onerror i.e override old definition....
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17163895
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
0
 
LVL 3

Author Comment

by:novice_expert
ID: 17165258
it doesn't work....its calling the old definition?
0
 
LVL 6

Accepted Solution

by:
SysTurn earned 50 total points
ID: 17165304
show me the line of code where you override the old function. May be you are adding () after function name, You shouldn't!
obj.onerror = function_name();

the right syntax is:
obj.onerror = function_name;

Kind Regards
Bakr
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
age from date of birth 4 42
Javasctipt 2 25
jquery selector - how to select all the td elements, that doesn't have an anchor element inside them 1 19
Jquery 2 11
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question