crizCraig
crizCraig

Reputation: 8897

Dynamically appended scripts not downloaded in Chrome

Does anybody know how to append scripts dynamically in Chrome? I copied the following code that works in FF 3.0 and IE7 but not Chrome.

    function include_dom(script_filename) {
        var html_doc = document.getElementsByTagName('head').item(0);
        var js = document.createElement('script');
        js.setAttribute('language', 'javascript');
        js.setAttribute('type', 'text/javascript');
        js.setAttribute('src', script_filename);
        html_doc.appendChild(js);
        return false;
    }

    var included_files = new Array();

    function include_once(script_filename) {
        if (!in_array(script_filename, included_files)) {
            included_files[included_files.length] = script_filename;
            include_dom(script_filename);
        }
    }

    function in_array(needle, haystack) {
        for (var i = 0; i < haystack.length; i++) {
            if (haystack[i] == needle) {
                return true;
            }
        }
        return false;
    }

I can see the code appended to the head element by running:

`document.getElementsByTagName('head').item(0).innerHTML;

within Chrome's inspector, however I can't see any of the scripts being requested in my HTTP sniffer.

Upvotes: 1

Views: 180

Answers (1)

crizCraig
crizCraig

Reputation: 8897

All right, this was actually Chrome caching more aggressively than FF and IE. Once I cleared Chrome's cache, the files were downloaded. FF and IE requested the files but didn't actually download them since the files were up to date. Chrome just doesn't ask for them at all.

Upvotes: 3

Related Questions