简体   繁体   English

通过带有参数的HTTP GET Request的Javascript

[英]Javascript through HTTP GET Request with parameters

I see in MathJax they include the script like this. 我在MathJax中看到它们包含这样的脚本。

<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? 有没有办法在javascript中获取config参数?

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. 获取该URL的唯一方法是搜索当前文档并找到特定的<script>标记,然后从script标记获取.src属性,然后对其进行解析以获取config参数。

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> 

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. 您可以使用正则表达式和plain-old-javascript来提取config参数,但是如果您使用的是jQuery,则可以使用更优雅的方式来隔离所需的元素。

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/ JSFiddle: https ://jsfiddle.net/vdqvjnbw/

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. 直到浏览器将这些字节拉入页面后,JS代码的二进制表示形式才会加载到内存中。 Meaning, that JS is basically just a text file out on the server until its downloaded by the browser and interpreted; 这意味着JS基本上只是服务器上的文本文件,直到浏览器下载并解释它为止; the JavaScript has no behavior until then. 直到那时,JavaScript都没有任何行为。

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: 但是,在页面内部-如果您想从script标签的src属性中删除该查询参数-您可以执行以下操作:

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. 此[unested]代码应为您提供一个包含密钥config的对象,该密钥config已为其设置了什么值。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM