egon12
egon12

Reputation: 810

Javascript through HTTP GET Request with parameters

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?

Upvotes: 1

Views: 171

Answers (3)

jfriend00
jfriend00

Reputation: 707258

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>

Upvotes: 1

Cody
Cody

Reputation: 10015

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.

Upvotes: 0

Julian
Julian

Reputation: 424

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/

Upvotes: 1

Related Questions