Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23713
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Why is it so convoluted to import a .js file in javascript? |
| Date | 2014-03-27 22:29 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <5419492.dk8GnlDV4K@PointedEars.de> (permalink) |
| References | <d1050ce2-5b06-4bb8-9d69-18cc325b30c0@googlegroups.com> <include-20140327003813@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> jonas.thornvall@gmail.com writes:
>> // load math.js and create an instance
>> var mathjs = require('math,js'),
>
> s/,/./
>
> The method »require« is a method of the modules API of
> Node.js,
ACK.
> it is not a part of standard JavaScript AFAIK.
There is no “standard JavaScript”; the various implementations/languages
that call themselves „JavaScript“ (most notably: JavaScript™, JScript, V8,
Opera ECMAScript, and KJS) are *implementations of* a(n Ecma) standard, the
ECMAScript Language Specification (ECMA-262). Conforming implementations of
ECMAScript *may extend* the Specification’s algorithms and syntax:
<http://PointedEars.de/es-matrix>
This function is not specified in ECMAScript (yet?), and AFAIK it is not
available in any „JavaScript“. In Node.js, it is a feature provided by the
host environment that runs V8 JavaScript:
<http://nodejs.org/api/modules.html>
>>Why can't a simple
>>import('../test/test.js'); or append('../test/test.js');
>>do?
>
> When JavaScript is running in a web browser, scripts can be
> loaded in HTML using
>
> <script type="text/javascript" src="../test/test.js"></script>
“script” elements can also be inserted dynamically, causing the script code
to be loaded, which is closest to Node.js's approach. I do not think this
is limited to HTML.
However, for this to work reliably, the host environment must support either
a way to determine when the script resource has been loaded (and parsed) –
this is available in a so far conforming implementation of W3C DOM Level 3
Events (WD) [1] – or synchronous XHR (which allows the normal program flow
to continue only after the resource has been loaded; the usual caveats
apply).
An example for the former is:
function require (source, callback)
{
var script = document.createElement("script");
script.addEventListener("load", callback);
script.src = source;
/* HTML5/WHATWG HTML only */
document.head.appendChild(script);
}
require("http://script.example/foo.js", function () {
console.log("Script loaded");
});
(There are libraries that implement and improve this or a similar approach;
so far none can be recommended, none can be recommended against. Use it and
them with caution.)
A proof of concept for the latter is provided by jsx.importFrom() and
jsx.importOnce() in JSX:object.js [2]
> . JavaScript does not have a means to import a file itself.
> Maybe because it does not want to include features specific
> to only some environments, such as filesystem access.
ACK.
________
[1] <http://www.w3.org/TR/DOM-Level-3-Events/#event-type-load>
[2] <http://PointedEars.de/wsvn/JSX/trunk/object.js>
(Note that jsx.require() does _not_ implement the asynchronous approach
yet.)
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-26 16:22 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 01:37 -0700
Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 11:45 +0000
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 09:36 -0700
Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 17:17 +0000
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 19:48 +0100
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 20:52 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:39 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:59 -0700
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 18:19 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 10:41 -0700
eval() vs. own expression parser (was: Why is it so convoluted to import a .js file in javascript?) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-28 19:11 +0100
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-28 21:43 +0100
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-29 18:57 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:51 -0700
Re: Why is it so convoluted to import a .js file in javascript? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-03-27 17:30 +0100
Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 09:39 +0100
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 21:12 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 07:11 -0700
Re: Why is it so convoluted to import a .js file in javascript? John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-28 14:30 +0000
Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-28 15:36 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:17 -0700
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:23 -0700
Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-29 16:42 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 09:02 -0700
Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-28 19:00 +0100
Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 11:34 -0700
Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-27 22:29 +0100
csiph-web