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

Add code to <head> of HTML

by KianV Mon Nov 18, 2019 7:28 am

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: 772
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080

Re: Add code to <head> of HTML

by Peter Mon Nov 18, 2019 9:10 am

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: 8
Joined: Sun Nov 17, 2019 11:38 am

Re: Add code to <head> of HTML

by KianV Mon Nov 18, 2019 1:09 pm

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.)
Return to Javascript

Who is online

Users browsing this forum: No registered users and 1 guest