Solved

Override function in javascript

Posted on 2006-07-23
5
3,612 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

839 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