I've sketched out a new programming language for client side scripting. I'm familiar with how to build a language, and most of the work that goes into it. I'm prepared for a long project but have a few questions about how to implement it into a HTML file. I'd like to implement something like the <script> tag in HTML, and I've found a few links that explain how to add a new tag to HTML. I figure developers will have to connect to a js file with the language parser (it's actually more than a parser, but that's besides the point) in the head of their HTML file, like so: <script src="my-lang.js"></script>
. Here are some of the links I've found:
https://blog.teamtreehouse.com/create-custom-html-elements-2
https://developers.google.com/web/fundamentals/web-components/customelements
Here are some of my questions, assuming I want my language between <my-language> tags:
createdCallback()
function that is called when a <my-language> tag is used. Assuming nobody adds a <my-language> tag with JavaScript later (which for this language that would be pointless) that callback should be called anytime the custom tag is used.Thanks for putting up with my silly questions. I like to dive into the deep end with this stuff even though I'm fairly new at this part of scripting. Basically, this is what I want the code to look like:
<!DOCTYPE html>
<html>
<head><script src="my-lang.js"></script></head>
<my-language>
//do some stuff in my language, like DOM editing.
//Just a replacement for JavaScript basically that doesn't serve much purpose.
</my-language>
</html>
Any suggestions would be appreciated. Thanks in advance!
Rather than using a new nonstandard tag in the HTML, I would recommend considering using a <script>
tag. Although <script>
tags are often for Javascript, this is not always the case. For example, one possible technique for the server to send data for the client's JS to parse is to put JSON in a tag like <script type="application/json">{"foo":"bar"}</script>
. Scripts with types other than type="javascript"
will not be attempted to be parsed as Javascript, but the data inside the tag can be retrieved with Javascript. You can do that by selecting the tag and then accessing its textContent
property. (The innerHTML
property is probably only appropriate when you deliberately want to retrieve HTML markup - otherwise, probably best to use textContent
)
You can use nearly the same technique, but rather than JSON.parse
ing the content of the <script>
tag, send it through your parser.
For example:
const tag = document.querySelector('script[type="myNewLanguage"]');
const scriptText = tag.textContent;
// use your parser to parse scriptText
scriptText.split('\n').forEach(line => {
console.log(line);
});
<script type="myNewLanguage">foo
bar
baz</script>