Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is loading dynamic javascript and CSS files a bad idea?

The reason i'm asking this is because i want to write my own javascript framework from scratch. But as my code scales so does my the amount of code to navigate through.

In order to keep a clear overview I thought it'd be wise to split my code into separate files and let the framework load the files by adding the files to the header of my document.

It's not that big yet (<50kb) but considering the fact that it could get bigger; is there a big performance penalty in doing this or is there a better way to do this?

Update @Antony

Bundling and minifying is definitely on my to-do list. I do expect a speed bump in doing so. Still, wouldn't adding files to the header dynamically during the parsing cause the browser to rethink it's parsing and painting process? Is there a penalty here?

function

If you came here looking for a way to load external files via javascript

var loadExternalFile = function (id, filename, filetype, target, callback) {
    if (typeof target === "undefined") {
        target = "head";
    }
    if (filetype === "js") { //if filename is an external JavaScript file
        var fileref = document.createElement('script');
        fileref.type = "text/javascript";
        fileref.src = filename;
        fileref.id = id;
        if (typeof callback === "function") {
            fileref.onload = callback;
        }
    }
    else if (filetype === "css") { //if filename is an external CSS file
        var fileref = document.createElement("link");
        fileref.rel = "stylesheet";
        fileref.type = "text/css";
        fileref.href = filename;
        fileref.id = id;
        if (typeof callback === "function") {
            fileref.onload = callback;
        }
    }
    if (typeof fileref !== "undefined") {
        document.getElementsByTagName(target)[0].appendChild(fileref);
    }
};

// Example
loadExternalFile("example_id", "js/examplefile.js", "js", "document.body");
like image 978
DerpyNerd Avatar asked Mar 04 '26 16:03

DerpyNerd


1 Answers

Loading anything using JS hides these resources from browser's preload scanner. Preload scanner runs very early, before any JS is ran at all, before even real DOM is constructed, and schedules resources to be loaded in a way that browser deems to be optimal.

Loading resources with JS might be justified in some situations, e.g.

  • not all files are loaded for all users (but keep in mind that media can be used on link), and they're large enough that bandwidth savings are more important than latency.
  • you've measured and found that the browser doesn't load these resources in sensible order (and some of unimportant resources are blocking loading of critical ones). You still might not need JS for it in some cases, e.g. unimportant styles can be in <link> in <body> (it's valid HTML now), theres' <link rel=preload as=…>, etc.

In older browsers (IE, Android) there are bugs in handling of load events for scripts and CSS, so use an existing loader that has already dealt with the edge cases.

like image 62
Kornel Avatar answered Mar 06 '26 04:03

Kornel