Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > object.prototype question

Reply
Thread Tools

object.prototype question

 
 
steve dp
Guest
Posts: n/a
 
      03-15-2006
Lets say we run: window.alert = function() { };

Is there anyway to 'restore' the original alert() method or is it gone
forever?

I know you can do window.alert = Window.prototype.alert, but lets say
you also set Window.prototype.alert = function() { } or lets say we're
in Opera, which doesnt have a Window "class".

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      03-15-2006

steve dp wrote:
> Lets say we run: window.alert = function() { };
>
> Is there anyway to 'restore' the original alert() method or is it gone
> forever?


Are you trying to keep a possibility to restore alert() or are you
trying to eliminate such possibility?

In any case by doing window.alert = function() { }; you are not
overriding the native code, you merely replace the reference. So it's
enough to:

window.nativeAlert = window.alert;
window.alert = function() { };
....
window.alert = window.nativeAlert;

If you want to shadow the native method, when my strike back hack
would be to create new hidden iframe - which is window by its nature -
and use its fresh 'non-spooffed' references.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      03-15-2006
steve dp wrote:
> Lets say we run: window.alert = function() { };
>
> Is there anyway to 'restore' the original alert() method or is it gone
> forever?


No, it hasn't 'gone' anywhere. You've just overridden the reference to it.
I guess a cynic might say that if you remove all references it'll be
swallowed by garbage collection but I doubt that can happen with
in-built host objects.


> I know you can do window.alert = Window.prototype.alert, but lets say
> you also set Window.prototype.alert = function() { } or lets say we're
> in Opera, which doesnt have a Window "class".


Store a reference to it, then restore it when you're finished. Play with this:

<p>Test window.alert</p>
<input type="button" value="test alert"
onclick="alert('hey, it worked');">

<p>Store a reference to window.alert</p>
<input type="button" value="store reference"
onclick="window.keptAlertRef = window.alert;">

<p>Over-ride window.alert</p>
<input type="button" value="over-ride alert"
onclick="window.alert = null;">

<p>Restore window.alert from stored reference</p>
<input type="button" value="restore reference"
onclick="window.alert = window.keptAlertRef;">



--
Rob
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-16-2006
RobG wrote:

> steve dp wrote:
>> Lets say we run: window.alert = function() { };
>>
>> Is there anyway to 'restore' the original alert() method or is it gone
>> forever?

>
> No, it hasn't 'gone' anywhere. You've just overridden the reference to
> it. I guess a cynic might say that if you remove all references it'll be
> swallowed by garbage collection but I doubt that can happen with
> in-built host objects.


It is just another host Function(-like) object that was created when the
script engine was invoked; its only specialty is that it _contains_ a call
to a built-in method. It is not the object referred to by `window' that
would be GC'd then, or the called built-in method, only one of `window''s
methods. So it is perfectly reasonable if that would be marked for GC
once there were no more references to it. It is therefore also perfectly
reasonable to assume that it would be restored if the document was
reloaded, which is in fact what happens.

BTW, I fail to find anything cynical about garbage collection other than
"Is it again /my/ turn to take out the garbage? (sigh)"


Regards,
PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57