1

Is it possible that JQuery to modify URL's in side a CSS or Javascript resource before its loads into the browser. Or at least as it loads in the browser?

URL usually pointing to resources, like images, fonts etc.

Basically I am thinking of this idea since I usually do large Single page web app/website which during development resources root path is relative, but in production the root path points to some other URL.

Is there something that solve this kind of problem? I am thinking like having a javascript that checks:

(psuedo-code)

var isDevMode = true;
if (isDevMode) {
 root_path = "/";
} else {
 root_path = "http://somewhere.com/"
}

So I just set it and all the path in my very big and complex HTML file, including the CSS in the page gets the right root path.

4
  • if I remember correctly, it is possible to load a css into a var of js, then you can use .replace(). Then append it to head like this Commented May 15, 2013 at 3:28
  • perhaps you could also try to rewrite it using paths like this background:url('./images/body_bg.png'); Commented May 15, 2013 at 3:35
  • whats on the server side? in asp.net its possible to override the Render method and regex for and replace links, but its a bit ugly. Commented May 15, 2013 at 3:58
  • @user1778606 There is no server side for the SPA I do, at least that is tightly integrated. our design just call on resources from external storage service Commented May 15, 2013 at 6:01

1 Answer 1

2

One way to modify your stylesheet paths inside your html would be to use the $("link") selector and modify it's html attribute. (code below)

To modify the actual paths for styles within a stylesheet that starts getting into jS CSS Parsing. You may want to take a look at the accepted answer here: CSS parser/abstracter? How to convert stylesheet into object

$(document).ready(function () {
    var isDevMode = true;
    var root_path = "";

    if (isDevMode) {
        root_path = "/";
    } else {
        root_path = "http://somewhere.com/";
    }

    $("link").each(function (index) {
        var existing_path = $(this).attr("href");
        $(this).attr("href", root_path + existing_path);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

see stackoverflow.com/questions/2126238/… ($('head').append('<link rel="stylesheet" type="text/css" href= 'css_path'>');) for loading the stylesheet? As long as its caching there shouldn't be too much page delay?

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.