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 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> References: <2d97db74-9dc2-4a99-bcf8-68944b7eef51@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn 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+&. […] 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). [referred to by – 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). 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.