document.head, document.body to attach scripts

后端 未结 4 879
死守一世寂寞
死守一世寂寞 2020-12-29 21:02

I have often used, and seen recommended, dom-access structures like this for adding content to pages dynamically:

loader = document.createElement(\'script\')         


        
4条回答
  •  难免孤独
    2020-12-29 21:58

    document.body is part of the DOM specification, I don't see any point why not to use it. But be aware of this:

    In documents with contents, returns the element, and in frameset documents, this returns the outermost element.

    (from https://developer.mozilla.org/en/DOM/document.body)

    document.head currently isn't defined in any DOM specification (apparently I was wrong on that, see Daniel's answer), so you should generally avoid using it.

提交回复
热议问题