// 创建一个 script 标签
const scriptElement = document.createElement('script');
// 添加一个回调函数,处理异步加载的脚本
const handleScriptLoad = (event) => {
// 判断当前请求的脚本是否已经加载完成
if (event.target.readyState === 'loaded') {
// 如果已经加载完成,则触发另一个回调函数
handleScriptDone();
}
};
// 添加一个回调函数,处理异步加载的样式
const handleStyleLoad = (event) => {
// 判断当前请求的样式是否已经加载完成
if (event.target.readyState === 'loaded') {
// 如果已经加载完成,则触发另一个回调函数
handleStyleDone();
}
};
// 将 script 标签添加到页面中
scriptElement.src = 'https://example.com/script.js';
scriptElement.onload = handleScriptLoad;
document.head.appendChild(scriptElement);
// 将 style 标签添加到页面中
const styleElement = document.createElement('style');
styleElement.textContent = 'body { background-color: red; }';
document.head.appendChild(styleElement);
// 添加一个回调函数,处理异步加载的样式
const handleStyleDone = () => {
// 样式加载完成后,将一个回调函数添加到样式中
styleElement.onload = handleStyleLoad;
};
// 处理异步加载的脚本
const handleScriptDone = () => {
// 脚本加载完成后,执行另一个回调函数
console.log('脚本加载完成');
};
没有搜到相关的文章