PaymentRequestEvent: phương thức openWindow()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is only available in Service Workers.

Phương thức openWindow() của giao diện PaymentRequestEvent mở URL được chỉ định trong một cửa sổ mới, chỉ khi URL đã cho cùng nguồn gốc với trang đang gọi. Trả về một Promise được giải quyết với tham chiếu đến một WindowClient.

Cú pháp

js
openWindow(url)

Tham số

url

URL để mở trong cửa sổ mới. Nó phải cùng nguồn gốc với trang đang gọi.

Giá trị trả về

Một Promise được giải quyết với tham chiếu đến WindowClient.

Ví dụ

js
self.addEventListener("paymentrequest", async (e) => {
  // …
  // Retain a promise for future resolution
  // Polyfill for PromiseResolver at link below.
  resolver = new PromiseResolver();

  // Pass a promise that resolves when payment is done.
  e.respondWith(resolver.promise);
  // Open the checkout page.
  try {
    // Open the window and preserve the client
    client = await e.openWindow(checkoutURL);
    if (!client) {
      // Reject if the window fails to open
      throw new Error("Failed to open window");
    }
  } catch (err) {
    // Reject the promise on failure
    resolver.reject(err);
  }
});

Xem Open the payment handler window to display the web-based payment app frontend để biết thêm chi tiết về cách sử dụng phương thức này.

Thông số kỹ thuật

Specification
Web-based Payment Handler API
# dom-paymentrequestevent-openwindow

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

Xem thêm