Javascript anatomy: Difference between revisions
From wikinotes
Line 88: | Line 88: | ||
= Web Browser = | = Web Browser = | ||
<blockquote> | |||
== Script Tags == | |||
<blockquote> | |||
One way that javascript can added to HTML within a <code><script></code> tag.<br> | |||
You can enter it directly, or set the <code>src=</code> parameter.<br> | |||
All HTML elements defined above the <code><nowiki><script</nowiki></code> is accessible within it (and not items below). | |||
<source lang="html4strict"> | |||
<script> | |||
function myFunction() { | |||
document.getElementById("demo").innerHTML = "Paragraph changed."; | |||
} | |||
</script> | |||
</source> | |||
<source lang="html4strict"> | |||
<script src="myscript.js"/> | |||
<script src="https://foo.com/js/foo.js"/> | |||
</source> | |||
</blockquote><!-- Script Tags --> | |||
== ES6 Modules == | |||
<blockquote> | <blockquote> | ||
Some features of javascript (ex. [[javascript modules|ES6 modules]]) do not work when loading webpages directly from the filesystem.<br> | Some features of javascript (ex. [[javascript modules|ES6 modules]]) do not work when loading webpages directly from the filesystem.<br> | ||
Line 165: | Line 187: | ||
# reload to get changes | # reload to get changes | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- ES6 Modules --> | |||
</blockquote><!-- Web Browser --> | </blockquote><!-- Web Browser --> | ||
Revision as of 12:16, 5 February 2023
NodeJS CLI
${PROJECT}/main.mjs
#!/usr/bin/env node import * as Process from 'node:process' import * as Path from 'node:path' const EXECUTABLE = Path.basename(Process.argv[1]); class CliCommand { help = false; verbose = false; static fromArgs() { var cmd = new CliCommand(); var argv = Process.argv.slice(2); while (argv.length) { var item = argv.shift(); switch (item) { case "-h": case "--help": cmd.help = true; break; case "-v": case "--verbose": cmd.verbose = true; break; default: console.error(`Invalid Param: '${item}'. See --help`); Process.exit(1); } } return cmd; } exec() { if (this.help) { showHelp(); Process.exit(0); } if (this.verbose) { console.debug("loggers set to stun"); } console.log(".. your program starts here"); } } function showHelp() { var msg = `${EXECUTABLE} [-h] [-v] ARGS: -h --help show this help message. EXAMPLE: ${EXECUTABLE} -h # show help `.trim() console.log(msg); } function main() { var cmd = CliCommand.fromArgs(); cmd.exec(); } main();./main.mjs -h # run
Web Browser
Script Tags
One way that javascript can added to HTML within a
<script>
tag.
You can enter it directly, or set thesrc=
parameter.
All HTML elements defined above the<script
is accessible within it (and not items below).<script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script><script src="myscript.js"/> <script src="https://foo.com/js/foo.js"/>ES6 Modules
Some features of javascript (ex. ES6 modules) do not work when loading webpages directly from the filesystem.
You can use a tiny toy web-server like python http or golang simple-go-web-server to load your javascript instead.${PROJECT}/index.html
<!doctype html public "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html lang="en"> <head> <title>My Website</title> <script type="module" src="main.js"></script> <!-- <link rel="stylesheet" href="stylesheet.css" /> --> </head> <body> <h1>My Title</h1> <p>Some paragraph</p> <div id="sum-div"> <button id="sum-btn">Add</button> <input class="addend" type="text" placeholder="1"> <input class="addend" type="text" placeholder="2"> <span>=</span> <input class="result" type="text" readonly="1"> </div> </body> </html>${PROJECT}/main.js
import * as Math from './modules/math.js' function sumClicked() { var sum_btn = document.getElementById("sum-div"); var addend_inputs = Array.from(sum_btn.querySelectorAll(".addend")); var addend_values = addend_inputs.map((input) => { return Number(input.value) }); var sum = Math.sum(...addend_values); var result_input = sum_btn.querySelector(".result"); result_input.setAttribute("value", String(sum)); } document.getElementById("sum-btn").addEventListener("click", sumClicked);${PROJECT}/modules/math.js
export function sum(a, b) { return a + b; } export function multiply(a, b) { return a * b; }
open 'http://localhost:3000' simple-go-web-server --hot # reload to get changes