Using document.head.appendChild() to append a script tag that has an SRC attribute?

前端 未结 2 1433
情话喂你
情话喂你 2021-02-07 11:07

Any reason why the following code isn\'t working?

alert(\"1\");
document.head.appendChild(\"

        
2条回答
  •  情话喂你
    2021-02-07 11:35

    This will do what the original poster wants without creating a new object.

    document.head.innerHTML += "";
    

    The script will be appended to the end of the existing head section which may not be what you want.

    Believe it or not this will put your script at the start of the head section without erasing the existing head section:

    document.head.innerHTML = "" + document.head.innerHTML;
    

提交回复
热议问题