Coding in JavaScript without external file

45 views
Skip to first unread message

Maysara Al Jumaily

unread,
Apr 13, 2024, 6:56:52 PM4/13/24
to PreTeXt support
Hello everyone,

My request is to directly code in pure JavaScript without the need to store the code in an external file. I tried the following but didn't work.
<interactive xml:id="my-sample-code" platform="javascript" width="90%" aspect="4:5" >
let x = 0;
console.log("Hello");
console.log(x+1);
</interactive>


Best regards,
Maysara Al Jumaily

Rob Beezer

unread,
Apr 14, 2024, 1:37:14 PM4/14/24
to pretext...@googlegroups.com
Dear Maysara Al Jumaily,

Thanks very much for the suggestion. Andrew S has done something very similar
just a few weeks ago:

https://github.com/PreTeXtBook/pretext/pull/2137

It is almost as you propose, but you would wrap the Javascript in a PreTeXt
"script" element.

So make sure you are totally up-to-date and try:

<interactive xml:id="my-sample-code" platform="javascript" width="90%"
aspect="4:5" >
<script>
let x = 0;
console.log("Hello");
console.log(x+1);
</script>
</interactive>

Let us know how that goes?

Rob



On 4/13/24 15:56, Maysara Al Jumaily wrote:
> Hello everyone,
>
> My request is to directly code in pure JavaScript without the need to store the
> code in an external file. I tried the following but didn't work.
> <interactivexml:id="my-sample-code"platform="javascript"width="90%"aspect="4:5">
> let x = 0;
> console.log("Hello");
> console.log(x+1);
> </interactive>
>
>
> Best regards,
> Maysara Al Jumaily
>
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com
> <mailto:pretext-suppo...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pretext-support/573ae50e-84ab-4e48-99d9-e04489e763b8n%40googlegroups.com <https://groups.google.com/d/msgid/pretext-support/573ae50e-84ab-4e48-99d9-e04489e763b8n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Maysara Al Jumaily

unread,
Apr 19, 2024, 10:32:41 PM4/19/24
to PreTeXt support
Hello Rob,

Thank you so much for the detailed and helpful explanation! It worked after the update I had to install. I now have a follow-up question: Suppose I have a JavaScript file name myscript.js that contains several pre-written functions to use throughout the book. For the sake of simplicity, let us only have a single function:
function print(data){
console.log(data);
}

I want to use this function without the need of specifying the source attribute, nor manually updating the index.html file to include the script. In other words, I want the following snippet to work:
<interactive xml:id="my-sample-code" platform="javascript" width="90%" aspect="4:5">
<script>
print("Hi");
</script>
</interactive>

I am hoping we could specify the script destination in docinfo.ptx, similar to how LaTeX commands are included in <macros> ... </macros>, but I wasn't able to get it to work.

Best regards,
Maysara Al Jumaily

Andrew Scholer

unread,
Apr 20, 2024, 11:09:43 PM4/20/24
to pretext...@googlegroups.com
That is not currently possible.

The best available mechanism for right now is to specify the source. Alternatively, you could make the library a module, place the file in with your static files, and use an import inside the script itself to access the code.

Andrew Scholer (he/him/his)
Computer Science Instructor/Program Chair
Chemeketa Community College


To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/c7695c83-0ad1-4abd-a6f5-9fb5a4fbb684n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages