Search code examples
javascriptmathjax

How to make the source(path) of MathJax.js environment dependent?


How do I specify the following requirement in my html?

When the computer has access to Internet, the source of MathJax.js is https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js;

When it is offline, and the system is Linux, the source is /MathJax/MathJax.js; if the system is Windows, it is /C:/MathJax/MathJax.js. (It would be even better if I can use environment variable in the local path.)


Solution

  • You can put MathJax in a location relative to your static HTML file:

    <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js"></script>
    <script>
    if( !window.MathJax ) {
        document.write(
            '<script type="text\/javascript" src="../lib/MathJax.js"><\/script>'
        );
    }
    </script>
    

    This way your implementation is independent of the OS.

    Another option would be to set up the location according to your browser URL:

    <script>
    var url= location.protocol == 'http:'? 
             "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js" :
             "../lib/MathJax.js";
    document.write(
        '<script type="text\/javascript" src="' + url + '"><\/script>'
    );
    </script>