PerformanceNavigationTiming: toJSON() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2021.
Phương thức toJSON() của giao diện PerformanceNavigationTiming là một serializer; nó trả về biểu diễn JSON của đối tượng PerformanceNavigationTiming.
Cú pháp
js
toJSON()
Tham số
Không có.
Giá trị trả về
Một đối tượng JSON là kết quả tuần tự hóa của đối tượng PerformanceNavigationTiming.
Ví dụ
>Dùng phương thức toJSON
Trong ví dụ này, gọi entry.toJSON() sẽ trả về biểu diễn JSON của đối tượng PerformanceNavigationTiming.
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
console.log(entry.toJSON());
});
});
observer.observe({ entryTypes: ["navigation"] });
Điều này sẽ ghi một đối tượng JSON như sau:
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
}
Để lấy chuỗi JSON, bạn có thể dùng trực tiếp JSON.stringify(entry); nó sẽ tự động gọi toJSON().
Thông số kỹ thuật
| Specification |
|---|
| Navigation Timing Level 2> # dom-performancenavigationtiming-tojson> |