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


Groups > comp.lang.javascript > #34174 > unrolled thread

Dynamically load js files.

Started byjonas.thornvall@gmail.com
First post2017-10-24 04:29 -0700
Last post2017-10-26 23:06 +0200
Articles 8 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#34174 — Dynamically load js files.

Fromjonas.thornvall@gmail.com
Date2017-10-24 04:29 -0700
SubjectDynamically load js files.
Message-ID<2d97db74-9dc2-4a99-bcf8-68944b7eef51@googlegroups.com>
Can you read in new js SRC files from javascript as you need them?
As i understand it all source files i loaded in the HTML header?

But if it was a big project and you wanted to group them, could you load a file that contain the group of the SRC js files you want to load?

Like a hierarki, it gets weird when there is more 10-15 js files to keep track.
Could you even load them dynamically using javascript? Well probably not.

Of course using frames there is no problem with such a hierarki, let them load eachother?

But i do not understand howto to it using div.


I understand if you find the question confusing, but how can i load a js SRC file on demand or a file that holds many <SCRIPT SRC=""> tags dynamically?


Must i really load a new HTML file "something like in a hidden frame"?

[toc] | [next] | [standalone]


#34175

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-10-24 15:00 +0200
Message-ID<XnsA81898AFD78FCeejj99@194.109.6.166>
In reply to#34174
jonas.thornvall@gmail.com wrote on 24 Oct 2017 in comp.lang.javascript:

> I understand if you find the question confusing, but how can i load a js
> SRC file on demand or a file that holds many <SCRIPT SRC=""> tags
> dynamically? 

I suppose you mean 'download' by 'load'?

Automatic downloading with a browser 
leads to all kinds of security issues.

However, downloading any file fromm the web can be done
easily with a cscript/wscript script in VBS [or JS].

Example in VBS [not tested!!!!!]:

function getJSfile(fileName, sourceURL, targetDir)
  dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
  dim bStrm: Set bStrm = createobject("Adodb.Stream")
  xHttp.Open "GET", sourceURL & fileName, False
  xHttp.Send
  with bStrm
    .type = 1 '''binary
    .open
    .write xHttp.responseBody
    .savetofile targetDir & fileName, 2 '''overwrite
  end with
end function

getJSfile "xxx.js", "http://any.com/", "D:\temp\"

==================

To detect inner <script src="...js"> URL you can easily write 
a Regex search code and downlowd them recursively with the above getJSfile.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#34176

FromDr.Kral@nyc.rr.com
Date2017-10-24 12:48 -0400
Message-ID<ncruucl86t93a88bhbnbr4pchfe2nbmgdh@4ax.com>
In reply to#34174
On Tue, 24 Oct 2017 04:29:18 -0700 (PDT), jonas.thornvall@gmail.com wrote
in <2d97db74-9dc2-4a99-bcf8-68944b7eef51@googlegroups.com>:

>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');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src","http://www.xxx.js");
  document.getElementsByTagName("head")[0].appendChild(fileref);  

Embed it inside a function call.

This is not my creation.

HTH

[toc] | [prev] | [next] | [standalone]


#34188

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-10-25 00:09 +0200
Message-ID<3758479.za9IddC03d@PointedEars.de>
In reply to#34176
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.

[toc] | [prev] | [next] | [standalone]


#34192

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2017-10-25 15:18 +0100
Message-ID<a971vctq7f4elbbrhdc2quo1vda391umi1@4ax.com>
In reply to#34188
On Wed, 25 Oct 2017 00:09:33 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Dr.Kral@nyc.rr.com wrote:
>^^^^^^^^^^^^^^^^^^
>It is considered polite here to post using one’s real name.
  <snip>

"I demand *evidence* supporting statements."

  John

[toc] | [prev] | [next] | [standalone]


#34193

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-10-25 17:27 +0200
Message-ID<XnsA819B18FF53E0eejj99@194.109.6.166>
In reply to#34192
John G Harris <niam@jghnorth.org.uk.invalid> wrote on 25 Oct 2017 in 
comp.lang.javascript:

> On Wed, 25 Oct 2017 00:09:33 +0200, Thomas 'PointedEars' Lahn
> <PointedEars@web.de> wrote:
> 
>>Dr.Kral@nyc.rr.com wrote:
>>^^^^^^^^^^^^^^^^^^
>>It is considered polite here to post using one’s real name.
>   <snip>
> 
> "I demand *evidence* supporting statements."

Do you think it is inconsiderately polite, 
or perhaps quitte conciderably impolite?


It rather might be evidently impolite to demand on Usenet.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#34217

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2017-10-26 19:56 +0100
Message-ID<itb4vc516egoe2eujcfkokb67ictcdepm7@4ax.com>
In reply to#34193
On Wed, 25 Oct 2017 17:27:18 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>John G Harris <niam@jghnorth.org.uk.invalid> wrote on 25 Oct 2017 in 
>comp.lang.javascript:
>
>> On Wed, 25 Oct 2017 00:09:33 +0200, Thomas 'PointedEars' Lahn
>> <PointedEars@web.de> wrote:
>> 
>>>Dr.Kral@nyc.rr.com wrote:
>>>^^^^^^^^^^^^^^^^^^
>>>It is considered polite here to post using one’s real name.
>>   <snip>
>> 
>> "I demand *evidence* supporting statements."
>
>Do you think it is inconsiderately polite, 
>or perhaps quitte conciderably impolite?
>
>
>It rather might be evidently impolite to demand on Usenet.

Did you guess who I was quoting ?

  John

[toc] | [prev] | [next] | [standalone]


#34218

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-10-26 23:06 +0200
Message-ID<XnsA81AEB02096BCeejj99@194.109.6.166>
In reply to#34217
John G Harris <niam@jghnorth.org.uk.invalid> wrote on 26 Oct 2017 in 
comp.lang.javascript:

>>> "I demand *evidence* supporting statements."
>>
>>Do you think it is inconsiderately polite, 
>>or perhaps quitte conciderably impolite?
>>
>>
>>It rather might be evidently impolite to demand on Usenet.
> 
> Did you guess who I was quoting ?

Immaterial.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web