Solved

Override function in javascript

Posted on 2006-07-23
5
3,607 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it doesn't work....its calling the old definition?
0
 
LVL 6

Accepted Solution

by:
SysTurn earned 50 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now