?
Solved

Override function in javascript

Posted on 2006-07-23
5
Medium Priority
?
3,623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

771 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