User:AzaToth/loadscript.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function loadscript(page) {
    var req;
    var res;
    url = 'http://en.wikipedia.org/w/index.php?title=' + page + '&action=raw&ctype=text/javascript';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else { return; }
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            if(req.getResponseHeader("Content-Type").search(/javascript/) != -1)
                res = req.responseText;
            else
                alert("Not a Javascript file");
        }
    };
    req.open("GET", url, true);
    req.send(null);
    document.eval(res);
}