Overwrite native function

B

Björn Langhof

Hello.

I'd like to overwrite a native function. It's the reload()-function of
the document.location-object. My idea:

document.location.reload = function(){alert('foo')};

So if the function document.location.reload() is called the alert should
appear. But it doesn't. The page is still reloaded.

Any suggestions how to solve this?

Background: I write a Firefox-Extension and want to change the behavior
of a web page.

A possible workaround is to trigger the onUnload-event of the
document-element. But this is also called when the page is left without
a reload.

Thanks for your help.

Björn
 
M

Michael Winter

Björn Langhof said:
I'd like to overwrite a native function.

Be aware that there are two variants of what you refer to as a "native
function". The first is properties of built-in objects (Object,
Function, String, etc.). The second is properties of host objects (DOM
nodes, the document object, etc.). Whilst some of the former are
read-only, most aren't. Of those that are, they are clearly specified as
such. However, attributes of the latter are at the whim of the host
itself and may vary: some properties be read-only or enumerable, for
example, in "A", but not in "B".
It's the reload()-function of the document.location-object.

That's a host object, therefore the possibility of success is limited.
My idea:

document.location.reload = function(){alert('foo')};

So if the function document.location.reload() is called the alert
should appear. But it doesn't. The page is still reloaded.

Then it's reasonable to conclude that the property is read-only and your
assignment was ignored.
Any suggestions how to solve this?

Background: I write a Firefox-Extension and want to change the
behavior of a web page.

There may be a workaround for extensions, but I wouldn't know. If you
haven't already, it would be worthwhile to post to a more specialised
community that deal specifically with Firefox extensions.

[snip]

Mike
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top