DocumentType: phương thức before()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Phương thức DocumentType.before() chèn một tập hợp các đối tượng Node hoặc chuỗi vào danh sách con của nút cha của DocumentType, ngay trước DocumentType. Các chuỗi sẽ được chèn dưới dạng các nút Text tương ứng.

Cú pháp

js
before(param1)
before(param1, param2)
before(param1, param2, /* …, */ paramN)

Tham số

param1, ..., paramN

Một tập hợp các đối tượng Node hoặc chuỗi để chèn.

Giá trị trả về

Không có (undefined).

Ngoại lệ

HierarchyRequestError DOMException

Được ném ra khi nút không thể được chèn tại điểm đã chỉ định trong cây phân cấp.

Ví dụ

Thêm một conditional comment

Các nút comment hợp lệ trước khai báo doctype nhưng không được khuyến nghị dùng vì chúng kích hoạt quirks mode trong IE. Tuy vậy, một conditional comment cũng hoạt động với IE:

js
let docType = document.implementation.createDocumentType("html", "", "");
let myDoc = document.implementation.createDocument("", "", docType);

docType.before(
  document.createComment("<!--[if !IE]> conditional comment <![endif]-->"),
);

myDoc.childNodes;
// NodeList [<!--[if !IE]> conditional comment <![endif]-->, <!DOCTYPE html>]

Thông số kỹ thuật

Specification
DOM
# ref-for-dom-childnode-before①

Tương thích trình duyệt

Xem thêm