its_me
its_me

Reputation: 11338

JS code loads stylesheet. How to modify JS, and reference stylesheet in html?

<script src="http://cdn.webrupee.com/js" type="text/javascript"></script>

I am localizing the WebRupee script above so that everything (JS, CSS, font files) is loaded right from my server.

Here goes the beautified code in the JS file:

var _wr_load = window.onload;
window.onload = function () {
    if (typeof (_wr_load) == "function") {
        _wr_load()
    }
    _wr_d = document;
    _wr_l(_wr_d);
    _wr_i(_wr_d.body);
    _wr_re(_wr_d.body)
};
_wr_l = function (f) {
    var d = f.createElement("link");
    d.type = "text/css";
    d.rel = "stylesheet";
    d.href = "http://cdn.webrupee.com/font";
    var e = f.getElementsByTagName("head")[0];
    e.appendChild(d)
};
_wr_i = function (g) {
    var c = g.childNodes;
    var f = c.length;
    for (var h = 0; h < f; h++) {
        if (c[h].nodeType == 3) {
            if (!c[h].nodeValue.match(/^[\s]*$/)) {
                r = c[h].nodeValue;
                r = r.replace(/\s(Rs|Rs\.)\s/gi, " Rs. ");
                r = r.replace(/^(Rs|Rs\.)\s/gi, " Rs. ");
                r = _we_reg(r, /\sRs\.[0-9]+\s/gi, /(Rs\.)/gi);
                r = _we_reg(r, /^Rs\.[0-9]+$/gi, /Rs\./gi);
                r = _we_reg(r, /^Rs\.[0-9,]+[0-9]$/gi, /Rs\./gi);
                r = _we_reg(r, /^Rs\.[0-9,]+[0-9]\s/gi, /Rs\./gi);
                r = _we_reg(r, /\sRs\.[0-9,]+[0-9]\s/gi, /Rs\./gi);
                r = _we_reg(r, /\sRs\.[0-9,]+[0-9]\./gi, /Rs\./gi);
                r = _we_reg(r, /^Rs\.[0-9,]+[0-9]\./gi, /Rs\./gi);
                r = _we_reg(r, /\sRs\.[0-9,]+[0-9]\//gi, /Rs\./gi);
                r = _we_reg(r, /^Rs\.[0-9,]+[0-9]\//gi, /Rs\./gi);
                r = _we_reg(r, /\sRs\.[0-9,]+[0-9]/gi, /Rs\./gi);
                c[h].nodeValue = r
            }
        } else {
            if (c[h].nodeName.toLowerCase() != "script") {
                _wr_i(c[h])
            }
        }
    }
};
_we_reg = function (f, a, b) {
    var c = new RegExp(a);
    var e = c.exec(f);
    while (e != null) {
        var d = String(e);
        d = d.replace(b, " Rs. ");
        f = f.replace(e, d);
        e = c.exec(f)
    }
    return f
};
_wr_re = function (k) {
    var c = 0;
    if (k.nodeType == 3) {
        var n = k.data.indexOf(" Rs. ");
        if (n >= 0) {
            var m = document.createElement("span");
            m.className = "WebRupee";
            var e = k.splitText(n);
            var o = e.splitText(5);
            var p = e.cloneNode(true);
            m.appendChild(p);
            e.parentNode.replaceChild(m, e);
            c = 1
        }
    } else {
        if (k.nodeType == 1 && k.childNodes && !/(script|style)/i.test(k.tagName)) {
            for (var l = 0; l < k.childNodes.length; ++l) {
                l += _wr_re(k.childNodes[l])
            }
        }
    }
    return c
};

The JavaScript code does a few things, and one thing of prime interest to me is that, it dynamically adds this in HTML:

<link type="text/css" rel="stylesheet" href="http://cdn.webrupee.com/font">

What I want to do is, add the link element myself, and remove the then unnecessary, relevant JavaScript code from the file.

As far as I see it, this is the one:

_wr_l = function (f) {
    var d = f.createElement("link");
    d.type = "text/css";
    d.rel = "stylesheet";
    d.href = "http://cdn.webrupee.com/font";
    var e = f.getElementsByTagName("head")[0];
    e.appendChild(d)
};

But simply removing the snippet from the JS file, breaks its functionality (i.e. replacing 'Rs.' with the new India Rupee symbol). So, what am I doing wrong?

Upvotes: 1

Views: 144

Answers (1)

Aidan Ewen
Aidan Ewen

Reputation: 13308

You have removed the line at the top which calls the function, right?

_wr_l(_wr_d);

(if you remove the function definition _wr_l() then any attempts to call that function will fail).

Upvotes: 1

Related Questions