Function overriding

H

howa

If I want to override default function, what can I do?

The following code work with IE7, but not FF2:

<html>

<script>

function alert(str) {
document.write('debug:'+str);
}

alert('hello');

</script>


</html>
 
R

RobG

If I want to override default function, what can I do?

If you mean can you assign new values to properties of built-in
objects, you can do that for those that allow it (i.e. aren't read-
only) by assigning a new value. However, it is not generally
considered a good idea to mess with properties of the window or global
object such as alert.

The following code work with IE7, but not FF2:

It is always good to describe what "work" and "not work" mean to you.

<html>
<script>

function alert(str) {

It may be better to reassign the value of window.alert as:

window.alert = function(str) {


if only to make it explicit that you intend doing that.

document.write('debug:'+str);
}

alert('hello');

</script>
</html>


I get the expected result in both browsers (IE 6, Firefox 2) - the
string "debug:hello" is written to the document. What do you get?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top