Add code to <head> of HTML

Using Javascript from SpiderBasic
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Add code to <head> of HTML

Post 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">
  MathJax.Hub.Config({
  tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
  ,displayAlign: "left" 
 });
</script>
<script src="/mathjaxsource.js" type="text/javascript"> </script>
Any guidance would be much appreciated.
Thanks,
Kian
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Add code to <head> of HTML

Post by Peter »

This should do the job:

Code: Select all

Procedure MathJaxCallback(ScriptName.s, Success)
  
  If Success
    Debug "MathJax is ready"
  Else
    Debug "Something went wrong"
  EndIf
  
EndProcedure

Define MathJaxConfig.s

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

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

LoadScript("/mathjaxsource.js", @MathJaxCallback()) ; <- LoadScript() exists since SB V2.30 beta 1
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Re: Add code to <head> of HTML

Post 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.)
Post Reply