PerformanceNavigationTiming: toJSON() 方法

Baseline 已廣泛支援

此功能已成熟,並且適用於多種裝置和瀏覽器版本。自 2021 年 10 月以來,它已在所有瀏覽器中可用。

PerformanceNavigationTiming 介面的 toJSON() 方法是一個 序列化器;它會返回 PerformanceNavigationTiming 物件的一個 JSON 表示形式。

語法

js
toJSON()

引數

無。

返回值

一個 PerformanceNavigationTiming 物件的 JSON 表示形式(序列化結果)。

示例

使用 toJSON 方法

在此示例中,呼叫 entry.toJSON() 將返回 PerformanceNavigationTiming 物件的一個 JSON 表示形式。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log(entry.toJSON());
  });
});

observer.observe({ entryTypes: ["navigation"] });

這將記錄一個類似如下的 JSON 物件

json
{
  "name": "https://en.wikipedia.org/wiki/Main_Page",
  "entryType": "navigation",
  "startTime": 0,
  "duration": 227.60000002384186,
  "initiatorType": "navigation",
  "nextHopProtocol": "h2",
  "renderBlockingStatus": "blocking",
  "workerStart": 0,
  "redirectStart": 4,
  "redirectEnd": 71.40000000596046,
  "fetchStart": 71.40000000596046,
  "domainLookupStart": 71.40000000596046,
  "domainLookupEnd": 71.40000000596046,
  "connectStart": 71.40000000596046,
  "connectEnd": 71.40000000596046,
  "secureConnectionStart": 71.40000000596046,
  "requestStart": 73.7000000178814,
  "responseStart": 102.90000000596046,
  "responseEnd": 105.2000000178814,
  "transferSize": 19464,
  "encodedBodySize": 19164,
  "decodedBodySize": 83352,
  "serverTiming": [
    {
      "name": "cache",
      "duration": 0,
      "description": "hit-front"
    },
    {
      "name": "host",
      "duration": 0,
      "description": "cp3062"
    }
  ],
  "unloadEventStart": 0,
  "unloadEventEnd": 0,
  "domInteractive": 178.10000002384186,
  "domContentLoadedEventStart": 178.2000000178814,
  "domContentLoadedEventEnd": 178.2000000178814,
  "domComplete": 227.60000002384186,
  "loadEventStart": 227.60000002384186,
  "loadEventEnd": 227.60000002384186,
  "type": "navigate",
  "redirectCount": 1,
  "activationStart": 0
}

要獲取 JSON 字串,您可以直接使用 JSON.stringify(entry);它會自動呼叫 toJSON()

規範

規範
導航計時 Level 2
# dom-performancenavigationtiming-tojson

瀏覽器相容性

另見