Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26562

Re: Dynamically add/remove code in browsers

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 <PointedEars@web.de>
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> (permalink)
References <m905b3$90j$1@dont-email.me> <11460823.sCPipJV62P@PointedEars.de> <m914le$e0f$1@speranza.aioe.org>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
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

Show key headers only | View raw


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:
> <http://calendar.perfplanet.com/2014/the-pain-of-duplicate-scripts/>
> 
>   (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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Dynamically add/remove code in browsers "Leonardo Azpurua" <leonardo@exmvps.org> - 2015-01-12 05:22 -0430
  Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 17:21 +0100
    Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-01-12 16:44 -0200
      Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 22:01 +0100
        Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com> - 2015-01-12 22:06 -0200
          Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 01:11 +0100
    Re: Dynamically add/remove code in browsers "Leonardo Azpurua" <leonardo@exmvps.org> - 2015-01-12 15:21 -0430
      Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 22:03 +0100
    Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-01-13 12:17 -0200
      Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 20:50 +0100

csiph-web