Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Base 8 is just like base 10, if you are missing two fingers. -- Tom Lehrer


devel / comp.lang.javascript / In-Page Script Modules

SubjectAuthor
o In-Page Script ModulesLawrence D'Oliveiro

1
In-Page Script Modules

<uqkia2$379pl$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18338&group=comp.lang.javascript#18338

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.javascript
Subject: In-Page Script Modules
Date: Thu, 15 Feb 2024 08:34:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <uqkia2$379pl$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 15 Feb 2024 08:34:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="06d12749e5def29a78db342a8b927267";
logging-data="3385141"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199cdAFragjB1MlZyDoSvvA"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:oRIebXtFlT3cx0dlNT30jOjoMiI=
 by: Lawrence D'Oliv - Thu, 15 Feb 2024 08:34 UTC

You can put a “<script type="module">” section directly in a web page.
But what does that do? It does two things:

* Enable strict mode on that script section
* Defer execution of that script section until the page has loaded.

What it doesn’t do:

* Turn the script section into a module.

You could say it does turn the code into a module, with its own global
namespace. But this module has no name, so there is no way to import
anything from it into other code on the page. Or is there?

It turns out you can export things from this module, by attaching them
to the global “window” object. Here’s an example, where I create a
pretend-module called “inpage” that contains the objects exported from
my in-page module:

<script type="module">
function doit(spanid)
{
document.getElementById(spanid).innerHTML += "Hi There.<br>\n"
} /*doit*/

window.inpage = {"doit" : doit}
</script>

That only contains a single object, but you can see how to add more in
the same way.

Now, elsewhere on the page, I can access the contents of this module
quite easily, e.g.

<p>Something should appear here → <span id="content"></span> ← watch this space.
<button onclick="inpage.doit(&quot;content&quot;)">Click Me</button>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor