JavaScript snippet: Remove base URL from link

I needed this function this morning so I thought I’d share it in case someone else does too.

?View Code JAVASCRIPT
function RemoveBaseUrl(url) {
    /*
     * Replace base URL in given string, if it exists, and return the result.
     *
     * e.g. "http://localhost:8000/api/v1/blah/" becomes "/api/v1/blah/"
     *      "/api/v1/blah/" stays "/api/v1/blah/"
     */
    var baseUrlPattern = /^https?:\/\/[a-z\:0-9.]+/;
    var result = "";
 
    var match = baseUrlPattern.exec(url);
    if (match != null) {
        result = match[0];
    }
 
    if (result.length > 0) {
        url = url.replace(result, "");
    }
 
    return url;
}

CSS auto reload with Django templates

Have you ever been in the situation where you’ve updated your CSS file but users’ web browsers haven’t automatically loaded the new one? The reason is because many web browsers cache the stylesheet for faster future reloading. Obviously you don’t want to have to get your users to Ctrl-F5 every time you update your stylesheet so here’s a little tip for making this automatic in your Django templates.

Read More →