Bạn cần phải đính kèm một xử lý sự kiện cho một trong hai phương pháp onload, trong các trình duyệt phù hợp với các tiêu chuẩn web, hoặc onreadystatechange, kiểm tra tài sản script.readyState nhận bằng "tải" hay "hoàn thành" , trong Internet Explorer.
Trước khi bạn nhận được thông báo rằng tập lệnh đã được tải, có thể bạn đang cố gắng truy cập các đối tượng, chức năng và thuộc tính chưa được khai báo hoặc tạo.
Dưới đây là một chức năng ví dụ, được chiết xuất từ các mô-đun bezen.dom.js trong Javascript library, bezen.org tôi:
var appendScript = function(parent, scriptElt, listener) {
// append a script element as last child in parent and configure
// provided listener function for the script load event
//
// params:
// parent - (DOM element) (!nil) the parent node to append the script to
// scriptElt - (DOM element) (!nil) a new script element
// listener - (function) (!nil) listener function for script load event
//
// Notes:
// - in IE, the load event is simulated by setting an intermediate
// listener to onreadystate which filters events and fires the
// callback just once when the state is "loaded" or "complete"
//
// - Opera supports both readyState and onload, but does not behave in
// the exact same way as IE for readyState, e.g. "loaded" may be
// reached before the script runs.
var safelistener = catchError(listener,'script.onload');
// Opera has readyState too, but does not behave in a consistent way
if (scriptElt.readyState && scriptElt.onload!==null) {
// IE only (onload===undefined) not Opera (onload===null)
scriptElt.onreadystatechange = function() {
if (scriptElt.readyState === "loaded" ||
scriptElt.readyState === "complete") {
// Avoid memory leaks (and duplicate call to callback) in IE
scriptElt.onreadystatechange = null;
safelistener();
}
};
} else {
// other browsers (DOM Level 0)
scriptElt.onload = safelistener;
}
parent.appendChild(scriptElt);
};
Để thích ứng với nhu cầu của bạn, bạn có thể thay thế các cuộc gọi đến catchError, bao bọc người nghe để đón và đăng nhập lỗi và sử dụng chức năng đã sửa đổi:
var appendScript = function(parent, scriptElt, listener) {
// append a script element as last child in parent and configure
// provided listener function for the script load event
//
// params:
// parent - (DOM element) (!nil) the parent node to append the script to
// scriptElt - (DOM element) (!nil) a new script element
// listener - (function) (!nil) listener function for script load event
//
// Notes:
// - in IE, the load event is simulated by setting an intermediate
// listener to onreadystate which filters events and fires the
// callback just once when the state is "loaded" or "complete"
//
// - Opera supports both readyState and onload, but does not behave in
// the exact same way as IE for readyState, e.g. "loaded" may be
// reached before the script runs.
var safelistener = function(){
try {
listener();
} catch(e) {
// do something with the error
}
};
// Opera has readyState too, but does not behave in a consistent way
if (scriptElt.readyState && scriptElt.onload!==null) {
// IE only (onload===undefined) not Opera (onload===null)
scriptElt.onreadystatechange = function() {
if (scriptElt.readyState === "loaded" ||
scriptElt.readyState === "complete") {
// Avoid memory leaks (and duplicate call to callback) in IE
scriptElt.onreadystatechange = null;
safelistener();
}
};
} else {
// other browsers (DOM Level 0)
scriptElt.onload = safelistener;
}
parent.appendChild(scriptElt);
};
Nguồn
2009-12-17 21:05:16
Kiểm tra câu trả lời liên quan này ra: ** [Thứ tự tải của các thẻ tập lệnh được thêm động] (http://stackoverflow.com/a/38840724/2247494) ** – jherax