yes - you can use the hundreds of different document object model commands that make up a significant fraction of the commands available in JavaScript.
The title threw me a little too, maybe ābetween the body tagsā would have been clearer. That said, isnāt this essentially what many JS frameworks are doing these days, in which case thereās a few out there to choose from ( and a newer one being announced everyday if weāre to believe the cynics ).
I was thinking some more like below. The only drawback is I have to provide the body tag an ID.
Source: DOM Scripting - Second Edition. Jeremy Keith.
Html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sequence into a sound</title>
</head>
<body id="page">
<script src="module.js"></script>
</body>
</html>
JavaScript:
window.onload = function () {
var para = document.createElement("p");
var body = document.getElementById("page");
var text = "Hello World";
var content = document.createTextNode(text);
para.appendChild(content);
body.appendChild(para);
};
I have only ever come across one script where I needed an onload but you can easily get around it if you do need more than one by using event listeners instead of event handlers.
For example the following will run both function1() and function2() when the onload event is triggered.
I know you like to say that, @felgall, but the OP explicitly said that the body was empty, so no <script> tags before </body> (which would make it an element inside the body).
I never intended to put script tags in the page: Creating the element will be in a function called by a button click event handler. I simply attempting to illustrate what my ideas are.
So any JavaScript in the page at all would be in the WRONG place as ALL JavaScript with very few exceptions is best placed in a separate file attached immediately before the </body> tag. I would assume a page where JavaScript is mentioned and it states that there is noting in the body that except for the script tag at the bottom of the body is implied as otherwise the page is a jumbled mess and if the JavaScript isnāt broken now it will be at some point.
JavaScript should never be mixed in with the HTML as:
it restricts you to about 2% of what JavaScript can do,
it doesnāt hide the JavaScript from other scripts you decide to add later so it can clash and break, and
you then need to jumble them together on every page instead of writing the JavaScript only once and linking it to all of the hundreds of pages.
I know of exactly two types of script that need to go in the <head> rather than the bottom of the body. One is a one line framebreaker script and the other is one that adds a class or classes to the <html> page so that the page can be styled differently based on what version of JavaScript is supported.
The OP just learned about appendChild(). I think we can ease off him about script placement⦠which is a performance optimization only, by the way, and not even the most important one. Fortunately the OP was able to solve most of his problem on his own, which is impressive considering the quality of help he was getting.
As for using the best Selector, itās easier using a browserās dev tools, but it still isnāt all that easy to get the ābestā that hopefully will be future proof with any minor changes