आप सबसे ब्राउज़रों के लिए स्क्रिप्ट तत्व का onload
घटना का उपयोग कर सकते हैं, और एक कॉलबैक तर्क का उपयोग करें:
संपादित करें: आप वास्तव में कोड के निष्पादन नहीं रोक सकता है जब आप इस तरह से स्क्रिप्ट लोड (और सिंक्रोनस अजाक्स अनुरोध करना ज्यादातर बार एक बुरा विचार है)।
लेकिन आप कर सकते हैं श्रृंखला कॉलबैक है, तो आप कुछ कोड है कि दोनों पर निर्भर करता है, Two.js और Three.js, अगर आप यह कर सकते श्रृंखला लोड हो रहा है कार्रवाई, उदाहरण के लिए:
loadScript('http://example.com/Two.js', function() {
// Two.js is already loaded here, get Three.js...
loadScript('http://example.com/Three.js', function() {
// Both, Two.js and Three.js loaded...
// you can place dependent code here...
});
});
कार्यान्वयन:
function loadScript(url, callback) {
var head = document.getElementsByTagName("head")[0],
script = document.createElement("script"),
done = false;
script.src = url;
// Attach event handlers for all browsers
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || // IE stuff...
this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
callback(); // execute callback function
// Prevent memory leaks in IE
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
IE के लिए, onreadystatechange
घटना बाध्य होने के लिए है।
क्या इस प्रश्न का एक संस्करण हजारों बार नहीं पूछा गया है? –