Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34188
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Dynamically load js files. |
| Date | 2017-10-25 00:09 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <3758479.za9IddC03d@PointedEars.de> (permalink) |
| References | <2d97db74-9dc2-4a99-bcf8-68944b7eef51@googlegroups.com> <ncruucl86t93a88bhbnbr4pchfe2nbmgdh@4ax.com> |
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