function addScript(url, id, callback) {
// Check if the script has already been added
if (!document.getElementById(id)) {
// Create a script element for the script
var script = document.createElement('script');
script.src = url;
script.id = id;
// Add the onload event handler
script.onload = function() {
// Call the callback function when the script has finished loading
if (typeof callback === 'function') {
callback();
}
};
// Add the script element to the head section of the page
document.head.appendChild(script);
}
}
console.log("initiate");
document.addEventListener("readystatechange", (event) => {
if (event.target.readyState === "interactive") {
console.log("Page ready");
addScript(
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js",
"jquery-script",
function () {
console.log("jquery loaded");
}
);
}
});
1条答案
按热度按时间ojsjcaue1#
您可以使用
script.onload
JavaScript来加载带有回调的脚本。在这里,您可以单独访问JS加载。
试试下面的代码