Skip to content

Better insert before document.documentElement.firstChild #71

@mathiasbynens

Description

@mathiasbynens

This kinda technique is used throughout has.js:

root = d.body || (function(){
    fake = true;
    return de.insertBefore(d.createElement("body"), de.firstChild);
}());

E.g. https://github.com/phiggins42/has.js/blob/master/detect/bugs.js#L102-105

Might be worthwhile to use de.firstElementChild || de.firstChild instead, in case the firstChild is a comment node and the browser doesn’t support inserting before comment nodes.

(Props to Paul Irish for pointing out the mistake in my <details>/<summary> feature test, and John-David Dalton for coming up with this elegant solution.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions