Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: restore window alert Date: Sat, 10 Sep 2016 16:26:21 +0200 Organization: PointedEars Software (PES) Lines: 62 Message-ID: <1760285.oMNUckLgyt@PointedEars.de> References: <18kx87e56fmrr.u0ghv1tkc6cu.dlg@40tude.net> <92e03e5f-e115-40d1-ba94-4b34459d8c14@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1473517584 31128 eJwFwYEBwCAIA7CXBrRFz1GB/09YwpDpJUSBw2k1++3Bdp8pZgJ12MxTO6ITtnD1VbiV3/UDJ8ARFQ== (10 Sep 2016 14:26:24 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 10 Sep 2016 14:26:24 +0000 (UTC) User-Agent: KNode/4.14.2 X-NNTP-Posting-Host: eJwVxMERwDAIA7CVGrBNM45Dj/1HyFUPMbXUBVHgcKJALwQSMZ7531Ycv+WTrUh+D7vc4D51ARJNEPU= Cancel-Lock: sha1:J4MW6+ijZGkRXjcR9JdciOrLBX0= X-User-ID: eJwFwQERACAIA8BKTB2TOMBJ/wj+czu8dZx+OBz0brAMdZ9dmi2dQUiaR9Czunx6DURm0twirGPHXQnlBzZ3FJg= Xref: csiph.com comp.lang.javascript:31302 Michael Haufe (TNO) wrote: > On Thursday, September 8, 2016 at 6:47:29 AM UTC-5, JJ wrote: >> On Thu, 8 Sep 2016 10:39:16 +1000, Andrew Poulos wrote: >> > Is there any way to reference the native window.alert once its been >> > overridden? >> >> Use: >> >> window.constructor.prototype.alert >> >> e.g.: >> >> window.constructor.prototype.alert.call(window, "the message"); >> >> or restore it as a different name for easy access later e.g.: >> >> window.alert2 = window.constructor.prototype.alert; >> window.alert2("the message"); > > […] I'm paranoid about using "window.constructor". I see it in my mind as: > [host object].constructor ACK. Also, this is a long-winded, error-prone way of writing Object.getPrototypeOf(window) [formerly, window.__proto__ in some DOM implementations]. > Is this "safe" to us like this? No. The safest approach is to create a Window object and copy the value of its “alert” property. An iframe, as you suggested, does not suffice, though, because that only gives you the “iframe” element object. Accessing the window object of the iframe document requires either support for the “contentWindow” property of the iframe object or for the “contentDocument” of the iframe object and the “defaultView” property of the iframe’s document object. As an alternative, a new window object can be created, the reference to its “alert” method retrieved, and its close() method be called: var w = window.open(); var _alert = w.alert; w.close(); Then either window.alert = _alert; window.alert(42); or Function.prototype.call.call(_alert, window, 42); (AFAIK, all host methods can be safely .call()ed this way, and with combinations of .call() and .apply().) -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.