
Skrypty nieblokujące | 11
wstrzyknięcie nastronę kodu JavaScript przy użyciu dynamicznego elementu
<script>
.
Oto prosty przykład:
var xhr = new XMLHttpRequest();
xhr.open("get", "file1.js", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
var script = document.createElement("script");
script.type = "text/javascript";
script.text = xhr.responseText;
document.body.appendChild(script);
}
}
};
xhr.send(null);
Kod ten wysyła żądanie
GET
poplik le1.js. Obsługa zdarzenia
onreadystatechange
spraw-
dza, czy stan
readyState
jest równy 4, anastępnie sprawdza ...