HTMLMediaElement:loadstart 事件
当浏览器开始加载资源时,会触发loadstart
事件。
语法
在诸如 addEventListener()
之类的 方法中使用事件名称,或设置事件处理程序属性。
js
addEventListener("loadstart", (event) => {});
onloadstart = (event) => {};
事件类型
一个通用的 Event
。
示例
实时示例
HTML
html
<div class="example">
<button type="button">Load video</button>
<video controls width="250"></video>
<div class="event-log">
<label for="eventLog">Event log:</label>
<textarea readonly class="event-log-contents" id="eventLog"></textarea>
</div>
</div>
JavaScript
js
const loadVideo = document.querySelector("button");
const video = document.querySelector("video");
const eventLog = document.querySelector(".event-log-contents");
let source = null;
function handleEvent(event) {
eventLog.textContent += `${event.type}\n`;
}
video.addEventListener("loadstart", handleEvent);
video.addEventListener("progress", handleEvent);
video.addEventListener("canplay", handleEvent);
video.addEventListener("canplaythrough", handleEvent);
loadVideo.addEventListener("click", () => {
if (source) {
document.location.reload();
} else {
loadVideo.textContent = "Reset example";
source = document.createElement("source");
source.setAttribute(
"src",
"https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm",
);
source.setAttribute("type", "video/webm");
video.appendChild(source);
}
});
结果
规范
规范 |
---|
HTML 标准 # event-media-loadstart |
HTML 标准 # handler-onloadstart |
浏览器兼容性
BCD 表格仅在浏览器中加载