URL: thuộc tính href

Baseline Widely available

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

Note: This feature is available in Web Workers.

Thuộc tính href của giao diện URL là một chuỗi chứa toàn bộ URL.

Giá trị

Một chuỗi.

Ví dụ

URL đầy đủ với đường dẫn

js
const url = new URL(
  "https://mdn.go-mizu.dev/vi/docs/Web/API/URL/href",
);
console.log(url.href); // https://mdn.go-mizu.dev/vi/docs/Web/API/URL/href

URL chỉ có origin

Với các scheme phân cấp như https:, pathname luôn phải không rỗng. Khi URL không có path (ví dụ, chỉ có origin), bộ phân tích URL sẽ đặt pathname thành "/", vì vậy href sẽ bao gồm một dấu gạch chéo cuối. Xem thuộc tính pathname để biết thêm chi tiết.

js
const url = new URL("https://mdn.go-mizu.dev");
console.log(url.href); // https://mdn.go-mizu.dev/

Thông số kỹ thuật

Specification
URL
# dom-url-href

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

Xem thêm