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


Groups > comp.lang.javascript > #26562

Re: Dynamically add/remove code in browsers

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Dynamically add/remove code in browsers
Date 2015-01-12 22:01 +0100
Organization PointedEars Software (PES)
Message-ID <28422568.EsDYu13dd2@PointedEars.de> (permalink)
References <m905b3$90j$1@dont-email.me> <11460823.sCPipJV62P@PointedEars.de> <m914le$e0f$1@speranza.aioe.org>

Show all headers | 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