Is Javascript/jQuery DOM creation safe until it's added to the document?

前端 未结 4 1893
半阙折子戏
半阙折子戏 2021-01-31 16:49

Please read this statement carefully: let\'s assume before ANY elements are added to the document all unsafe elements in $dom have

4条回答
  •  醉梦人生
    2021-01-31 17:23

    It seems that the script will not work as long as it is not appended to DOM.

    $(function ()
    {
        var ss = document.createElement('script');
        var scr = 'alert("bah");';
        var tt = document.createTextNode(scr);
        ss.appendChild(tt);
        var hh = document.getElementsByTagName('head')[0];
        //hh.appendChild(ss);
    });
    

    And

    $(function ()
    {
        var ss = document.createElement('script');
        var scr = 'alert("bah");';
        var tt = document.createTextNode(scr);
        ss.appendChild(tt);
        var hh = document.getElementsByTagName('head')[0];
        hh.appendChild(ss);
    });
    

提交回复
热议问题