Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Dynamically add/remove code in browsers Date: Mon, 12 Jan 2015 22:01:13 +0100 Organization: PointedEars Software (PES) Lines: 42 Message-ID: <28422568.EsDYu13dd2@PointedEars.de> References: <11460823.sCPipJV62P@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1421096498 32590 eJwNxcEBwCAIA8CVUEgq49Ag+4/Q3ufgXNQTBAOD+ZdfdbFyhJ2VatxJN8iN1Rxon+NpfFd8Li8RPg== (12 Jan 2015 21:01:38 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 12 Jan 2015 21:01:38 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQERADEIAzBLbNDC5HDw9S/hEzgPJ4NgQNBcnjbO3WpEqsJp16079bYw/GK7uJPEPY/m80mASrapH0NJFXM= Cancel-Lock: sha1:bm0XBUorItntry2qy5/+2kgWu7I= X-NNTP-Posting-Host: eJwNwokRwDAIA7CVeO0wDtBm/xHak9KhWAYSkfd3FqbAlMoE+Xpsldn2w4TgMGkuA0Zjsz/76g/s Xref: csiph.com comp.lang.javascript:26562 Joao Rodrigues wrote: > On 12/01/2015 14:21, Thomas 'PointedEars' Lahn wrote: >> If you are accessing the “script” element by object reference, so it does >> not need an ID. > > However, what if there are other scripts in the document? Then a dynamically inserted “script” element with an ID would be an additional problem because IDs must be unique. > And what if the new script element already exists? Then, an ID would be of > great value to optimization. On the contrary. You would need to determine if the ID was already used by an element and try a different one until you find an unused one. And for what? If you have a reference to the object that represents the “script” element that you inserted before, you can simply reuse that reference (if you think you need to remove it). No ID necessary, and no document.getElementById() calls necessary. > All those questions would be better addressed by the following approach: > > > (function(d, s, id){ > var js, fjs = d.getElementsByTagName(s)[0]; > if (d.getElementById(id)) {return;} > js = d.createElement(s); js.id = id; > //js.src = "//localhost/test.js"; > fjs.parentNode.insertBefore(js, fjs); > }(document, 'script', 'dynScript')); This code is addressing a different use-case, and it is adressing it most poorly. Do not believe everything you read. Please trim your quotes to the relevant minimum. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.