使用JavaScript在head标记之前插入html(不在它们之间或它们之间)

使用JavaScript在head标记之前插入html(不在它们之间或它们之间),javascript,Javascript,我需要在html标记和doctype标记(如果存在)之前插入注释,如 如何在指定标签前插入“before” 不保证浏览器支持,但这在Firefox和Chrome中有效: var comment = document.createComment("HelloWorld"); document.insertBefore(comment, document.firstChild); 仅供参考,在解析HTML后,不要在“标记”之前插入。在DOM节点之前插入DOM。我将使用document.inser

我需要在html标记和doctype标记(如果存在)之前插入注释,如


如何在指定标签前插入“before”

不保证浏览器支持,但这在Firefox和Chrome中有效:

var comment = document.createComment("HelloWorld");

document.insertBefore(comment, document.firstChild);

仅供参考,在解析HTML后,不要在“标记”之前插入。在DOM节点之前插入DOM。

我将使用
document.insertBefore(comment,document.getElementsByTagName('html')[0]
,但它不太可靠,显然可能会在html节点后插入。+1对您来说!您不能在服务器端执行此操作吗?您必须在客户端执行此操作吗?是的,在测试浏览器条件以表明其要求后,它需要在客户端执行。
document.getElementsByTagName("html")[0].setAttribute("id", "something");
var comment = document.createComment("HelloWorld");

document.insertBefore(comment, document.firstChild);