Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34188
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.mb-net.net!open-news-network.org!.POSTED.124.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Dynamically load js files. |
| Date | Wed, 25 Oct 2017 00:09:33 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 66 |
| Message-ID | <3758479.za9IddC03d@PointedEars.de> (permalink) |
| References | <2d97db74-9dc2-4a99-bcf8-68944b7eef51@googlegroups.com> <ncruucl86t93a88bhbnbr4pchfe2nbmgdh@4ax.com> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| Injection-Info | gwaiyur.mb-net.net; posting-host="124.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.236.124"; logging-data="4769"; mail-complaints-to="abuse@open-news-network.org" |
| User-Agent | KNode/4.14.2 |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| 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== |
| Cancel-Lock | sha1:MF25oq+VnV3Ix9YZNhzmNZ1Dj3k= |
| X-User-ID | U2FsdGVkX1/0DkOW1R3Rwlie6LRDQ8DS2PZfRhawKTtmnVsfmRvaWw== |
| Xref | csiph.com comp.lang.javascript:34188 |
Show key headers only | View raw
Dr.Kral@nyc.rr.com wrote:
^^^^^^^^^^^^^^^^^^
It is considered polite here to post using one’s real name.
> […] jonas.thornvall@gmail.com wrote […]:
>>Can you read in new js SRC files from javascript as you need them?
> Yes.
>
> I have found that this works:
>
> var fileref=document.createElement('script');
In XHTML, it is necessary to call the createElementNS() method instead.
It is also a good idea to verify the return value.
> fileref.setAttribute("type","text/javascript");
> fileref.setAttribute("src","http://www.xxx.js");
It is unnecessary to call the less compatible setAttribute() method:
fileref.type = "…";
fileref.src = "…";
This has been so even before HTML5.
> document.getElementsByTagName("head")[0].appendChild(fileref);
(document.head || document.getElementsByTagName("head")
[0)].appendChild(fileref);
is more efficient in modern HTML user agents, per HTML5. Also, “script”
elements can equally be appended to be “body” element in a more compatible
and even more efficient way (the “document.body” property has been available
for more than 17 years now).
<https://html.spec.whatwg.org/multipage/dom.html#document>
[referred to by <https://www.w3.org/TR/2017/REC-html51-20171003/webappapis.html#event-handlers-on-elements-document-objects-and-window-objects> – note the date.]
HTML5 also specifies the behavior of user agents to fire specific events
related to loading scripts, which makes it possible to react to a script
having been loaded or impossible to be loaded in a standards-compliant way.
This makes it possible to organize source code into modules that are loaded
on an as-needed basis even though the language standard does not provide
such (yet).
<https://www.w3.org/TR/2017/REC-html51-20171003/webappapis.html#webappapis-scripting>
Also note that “fileref” is a misnomer. On the Web, we are talking about
_resources_ for which there does not have to be a corresponding file in a
filesystem.
> Embed it inside a function call.
The use of the verb “embed” for this is questionable. “Use” would be more
appropriate.
> This is not my creation.
See also my jsx.loadScript(), and jsx.define() and jsx.require(), in
JSX:object.js (see sig), and similar approaches like RequireJS.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | 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
Dynamically load js files. jonas.thornvall@gmail.com - 2017-10-24 04:29 -0700
Re: Dynamically load js files. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-24 15:00 +0200
Re: Dynamically load js files. Dr.Kral@nyc.rr.com - 2017-10-24 12:48 -0400
Re: Dynamically load js files. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-25 00:09 +0200
Re: Dynamically load js files. John G Harris <niam@jghnorth.org.uk.invalid> - 2017-10-25 15:18 +0100
Re: Dynamically load js files. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-25 17:27 +0200
Re: Dynamically load js files. John G Harris <niam@jghnorth.org.uk.invalid> - 2017-10-26 19:56 +0100
Re: Dynamically load js files. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-26 23:06 +0200
csiph-web