Page 1 of 1

Add code to <head> of HTML

Posted: Mon Nov 18, 2019 7:28 am
by KianV
Hi there,
I am putting together a program which uses MathJax to display equations, which needs a couple of short scripts inserted into the <head> portion of the page's HTML.
Up to now I have been manually inserting the scripts into the compiled app, but was wondering if there is a way of doing this directly from SpiderBasic.
I don't know if it makes any difference, but the scripts are as follows:
Code: Select all
<script type="text/x-mathjax-config">
  tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
  ,displayAlign: "left"
<script src="/mathjaxsource.js" type="text/javascript"> </script>

Any guidance would be much appreciated.

Re: Add code to <head> of HTML

Posted: Mon Nov 18, 2019 9:10 am
by Peter
This should do the job:

Code: Select all
Procedure MathJaxCallback(ScriptName.s, Success)
  If Success
    Debug "MathJax is ready"
    Debug "Something went wrong"

Define MathJaxConfig.s

MathJaxConfig = "<script type='text/x-mathjax-config'>" +
                "  MathJax.Hub.Config({" +
                "  tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}" +
                "  ,displayAlign: 'left'" +
                " });" +

! $("head").append(v_mathjaxconfig);

LoadScript("/mathjaxsource.js", @MathJaxCallback()) ; <- LoadScript() exists since SB V2.30 beta 1

Re: Add code to <head> of HTML

Posted: Mon Nov 18, 2019 1:09 pm
by KianV
Thanks Peter. That works perfectly. :D
The only thing that I had to change was the link in the 'src=' section. The link originally referred to an external script which uses 'document.write' to load the source location. (This was done to avoid having to manually update a huge swathe of pages when the repository moved.)