1

I see in MathJax they include the script like this.

<script type="text/javascript" src="path-to-MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

Is there a way to get the config parameter in javascript?

3 Answers 3

1

The only way to get that URL is to search the current document and find that particular <script> tag and then get the .src property from the script tag and then parse it to get the config parameters.

Scripts are loaded into the global browser namespace and don't have any properties or variables that are unique to a particular script. You could use something like this:

function findScript(tagToMatch) {
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
        if (scripts[i].src.indexOf(tagToMatch) !== -1) {
            // scripts[i].src is the full URL
            return scripts[i].src;
        }
    }
}

And, then you could use that generic function to find your particular tag and parse out the config value like this:

function findConfig() {
    var url = findScript("/MathJax.js?"), matches;
    if (url) {
        matches = url.match(/[&?]config=([^&$]+)/);
        if (matches) {
            return matches[1];
        }
    }
    return null;
}

var cfg = findConfig();

And, here's a working snippet:

function findScript(tagToMatch) {
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
        if (scripts[i].src.indexOf(tagToMatch) !== -1) {
            // scripts[i].src is the full URL
            return scripts[i].src;
        }
    }
}

function findConfig() {
    var url = findScript("/MathJax.js?"), matches;
    if (url) {
        matches = url.match(/[&?]config=([^&$]+)/);
        if (matches) {
            return matches[1];
        }
    }
    return null;
}

document.write(findConfig());
<script type="text/javascript" src="path-to-MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

It's not a hack. It's how you find the full URL for a given script. There is no feature built into the browser to do what you asked so this is how you search the system to find it. You call it a hack if you want, but it's how you solve this particular problem.
1

You can use regular expressions and plain-old-javascript to extract the config parameter, but if you're using jQuery there are more elegant ways of isolating the element you need.

function extractMathJaxConfig() {
    var scripts = document.getElementsByTagName("script")
    var regex = /config=([^&]+)/
    for (var i = 0; i < scripts.length; ++i) {
        var src = scripts[i].src;
        if (src.indexOf("MathJax.js") != -1) {
            var results = regex.exec(src);
            if (results) return results[1];
        }
    }
}

console.log(extractMathJaxConfig());

JSFiddle: https://jsfiddle.net/vdqvjnbw/

Comments

0

You won't get that parameter via your script that you're requesting in that script tag, here's why:

The binary representation of the JS code will not be loaded into memory until the browser has pulled those bytes into the page. Meaning, that JS is basically just a text file out on the server until its downloaded by the browser and interpreted; the JavaScript has no behavior until then.

However, inside of your page -- if you'd like to strip that query param from the src attribute of your script tag -- you can do something like this:

function getURIComponents(uri) {
    var a = document.createElement('a');
    a.href = uri;
    return {
        origin: a.origin,
        search: a.search
    };
}

function getParams(uri) {
    var c = getURIComponents(uri);
    var pairs = c.search.split('?')[1].split('&');
    var params = {};

    for (var i = 0, len = pairs.length; i < len; i++) {
        var pair = pairs[i].split('=');
        var name = pair[0];
        var value = pair[1];
        params[name] = value;
    }

    return params;
}

getParams(document.getElementByTagName('script').src);

This [untested] code should give you an object containing a key config with what value has been set for it.

Hope this helps.

4 Comments

But I can put code that provided by Julian to get the config in MathJax.js. The code is inside MathJax.js. Isn't it the javascript that included is also executed in page?
Yeah... You definitely could have the code inside of the body of the requested script -- I guess I assumed your intentions were for some sort of dynamic control before sending the request for the script. Either way, as an Architect, I would never want to rely on this value from within this script. But you could do this, though, everything breaks down if you start loading your scripts with something other than a <script> tag.
Well my itention is a litle bit crazier than that. I hope I can find a way to send POST Request to Javascript static files, so I don't need a server. But when I know it's kind of a hack, well I can't do that. But thanks for the sugestion. I wont use this technique for now.
@gilbertxenodike, apologies for the latency. If that is your goal, I would play around with two things: First, perhaps you can benefit from using a SharedWorker (see [github.com/cScarlson/worker.io][1] source). Second, you could potentially use AngularJS / Some MVC framework to create 'json' views -- if you need that control on the front-end. I've thought about this much, but have not ever POC'd this. Either way, I think you're not going to be able to get away from a server. Sounds very interesting what you're exploring, though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.