这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
Report
接口在
Reporting API
represents a single report.
Reports can be accessed in a number of ways:
ReportingObserver.takeRecords()
method — this returns all reports in an observer's report queue, and then empties the queue.
reports
parameter of the callback function passed into the
ReportingObserver()
constructor upon creation of a new observer instance. This contains the list of reports currently contained in the observer's report queue.
Report-To
HTTP header.
Report.body
只读
ReportBody
object containing the detailed report information.
Report.type
只读
deprecation
or
intervention
.
Report.url
只读
The URL of the document that generated the report.
This interface has no methods defined on it.
This interface has no events that fire on it.
In our deprecation_report.html example, we create a simple reporting observer to observe usage of deprecated features on our web page:
let options = {
types: ['deprecation'],
buffered: true
}
let observer = new ReportingObserver(function(reports, observer) {
reportBtn.onclick = () => displayReports(reports);
}, options);
We then tell it to start observing reports using
ReportingObserver.observe()
; this tells the observer to start collecting reports in its report queue, and runs the callback function specified inside the constructor:
observer.observe();
Because of the event handler we set up inside the
ReportingObserver()
constructor, we can now click the button to display the report details.
The report details are displayed via the
displayReports()
fuction, which takes the observer callback's
reports
parameter as its parameter:
function displayReports(reports) {
const outputElem = document.querySelector('.output');
const list = document.createElement('ul');
outputElem.appendChild(list);
for(let i = 0; i < reports.length; i++) {
let listItem = document.createElement('li');
let textNode = document.createTextNode('Report ' + (i + 1) + ', type: ' + reports[i].type);
listItem.appendChild(textNode);
let innerList = document.createElement('ul');
listItem.appendChild(innerList);
list.appendChild(listItem);
for (let key in reports[i].body) {
let innerListItem = document.createElement('li');
let keyValue = reports[i].body[key];
innerListItem.textContent = key + ': ' + keyValue;
innerList.appendChild(innerListItem);
}
}
}
reports
parameter contains an array of all the reports in the observer's report queue. We loop over each report using a basic
for
loop, then iterate over each entry of in the report's body using a
for...in
structure, displaying each key/value pair inside a list item.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Reporting API
The definition of 'Report' in that specification. |
编者草案 |
No compatibility data found. Please contribute data for "api.Report" (depth: 1) to the MDN 兼容性数据存储库 .