Javascript – SCRIPT5045: Assignment to read-only properties is not allowed in strict mode

javascript

I'm getting SCRIPT5045: Assignment to read-only properties is not allowed in strict mode in IE 11 (latest Chrome works fine) in reference to the line

A.doc.head = A.doc.getElementsByTagName('HEAD')[0];.

I'm confused on how to fix it. I've included what should be the relevant code below.

(function (win, doc, arg) {
    'use strict';
    var A = win[arg.prefix] = {
        'win': win,
        'doc': doc,
        'arg': arg,
        'stu': {},
        'fun': (function () {
            return {
                init: function () {
                    var scripts = A.doc.getElementsByTagName('SCRIPT'),
                        n = scripts.length,
                        i;
                    for (i = 0; i < n; i = i + 1) {
                        if (scripts[i].src.match(A.arg.src)) {
                            A.arg.script = scripts[i];
                            A.arg.options = A.fun.options();
                            break;
                        }
                    }
                    A.doc.head = A.doc.getElementsByTagName('HEAD')[0];
                    A.fun.structure();
                },
                // more functions
        }())
    };
    A.fun.init();
}(window, document, {
    'prefix': 'accescape_' + new Date().getTime(),
    'src': '/widget.js',
    'defaults': {
        'language': 'en'
    }
}));

Best Answer

document.head is a read-only property. If you want to shim it for oldIE, you'd better test for its nonexistence first:

if (!doc.head)
    doc.head = doc.getElementsByTagName("head")[0];