Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: intercept an alert Date: Mon, 12 May 2014 13:30:47 +0200 Organization: PointedEars Software (PES) Lines: 86 Message-ID: <2021642.a5K4omDOc0@PointedEars.de> References: <862dneogqrcsze3OnZ2dnUVZ_gCdnZ2d@westnet.com.au> <3910370.hJEAF5dPUy@PointedEars.de> <8761lb47e7.fsf@gmail.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 1399894258 14873 eJwNyskBwDAIA7CVMIch41Aa9h+h1VthBCedQY+Npb6aYVaCuo8mfE7PnrpDNkuYWWwbAfwfHwPDEE0= (12 May 2014 11:30:58 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 12 May 2014 11:30:58 +0000 (UTC) User-Agent: KNode/4.12.4 X-User-ID: eJwNzAEBwDAIAzBL26DlyHkZ+JfwR0Bg3Kxwgo7BDJV5zBpP8JCqoG7zXKOm8nYp7V3pITWnolAYBTbXv3xf4RXi Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Thomas 'PointedEars' Lahn writes: >> Andrew Poulos wrote: >>> if (!window._alert) { >>> window._alert = window.alert; >>> } >>> >>> window.alert = function(msg){ >>> if (msg.indexOf('is empty') != -1) { >>> return false; >>> } else { >>> return window._alert.apply(this, arguments); >>> } >>> }; >>> >>> Is this the way to go? >> >> Not quite. You should not augment the host object referred to by >> “window” with an “_alert” property (modifying its “alert” property is bad >> enough, but >> I cannot see a better alternative here). Use a variable or a property of >> your own object instead. > > Nonsense. > > It's perfectly fine to do that. > > People tag things on to "window" in each and every JS project. People like you who are lacking the minimum clue (on host objects). We have discussed this here ad nauseam: When you assign to a property of a host object, there is no telling what may happen because you cannot know the actual runtime environment (you can limit the runtime environments that you support, but that will only move the problem to the future, when there will be a new runtime environment that you did not consider; see jQuery as an example for that not-recommended approach). You can be lucky and the host object does not have a property of that name *and* allows augmentation. But there are other possibilities to consider: A. The host object does not have such a property and does not allow augmentation. a. The assignment fails silently. The .apply() call will fail because “undefined has no properties”. b. The assignment throws a recoverable exception. Result like with (a) unless caught before. c. The assignment throws an unrecoverable exception (fatal error). The program stops at the assignment with an error. MSHTML is known for either (b) or (c) when the property name is that of an ID or name in the document. B. The host object already has such property with that name. a. to c. like with (A), only that for (Aa) there is the additional possibility that the .apply() call may either do something unintended because the property value is kept (and another method is called), or that it fails because the still-unchanged property value is not callable (it could be a primitive value or refer to an object that does not implement [[Call]]). d. Modifying the property is successful but causes an unintended action because of a built-in setter, or it breaks some other functionality that the host object provides. Whereas “has a property” means “has an own property with that name or inherits one with that name through the prototype chain” here. Subjecting a property of a host object (or a host object itself) to conversion to Boolean, particularly supposed-to-be methods, or using a property of a host object in a type-converting test, as above, can be error- prone, too. (MSHTML is known for that.) See the ECMAScript Language Specification, 5.1 Edition, section 8.6.2, for details. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.