L
lovecreatesbeauty
I want to use an individual function to load a page in ajax way.
If that page refers to external script files, the function calls to
the external script fail at the first round only
.
If I put a similar alert() call for example in the following code at
line no. 28, then those function calls to external script won't fail.
Why it ever failed? It didn't get enough time to load the external
script or what? But an alert() gives it the time? I tried using
setTimtout with no lucky.
/* ajax(elmId, method, url)
* Ajax function with javascript on loaded page enabled
* with code stolen online
* jhlicc@{gmail,hotmail}.com, 20110626
*/
function ajax(elmId, method, url)
{
var xhr, elm;
xhr = new XMLHttpRequest();
elm = document.getElementById(elmId);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status == 200){
var s1, n1;
elm.innerHTML = xhr.responseText;
s1 = elm.getElementsByTagName("script");
n1 = s1.length;
for (var i = 0; i != n1; i++){
var s2;
s2 = document.createElement("script");
s2.type = "text/javascript";
if (s1.src) s2.src = s1.src;
if (s1.text) s2.text = s1.text;
elm.appendChild(s2);
if (s1.src){
/*Line#28*/ alert(src_appended);
}
}
for (var i = 0; i != n1; i++){
elm.removeChild(elm.getElementsByTagName("script")[0]);
}
} else {
elm.innerHTML = xhr.status + " " + xhr.statusText;
}}}
xhr.open(method, url);
xhr.send();
}
Thank you for your time
If that page refers to external script files, the function calls to
the external script fail at the first round only
If I put a similar alert() call for example in the following code at
line no. 28, then those function calls to external script won't fail.
Why it ever failed? It didn't get enough time to load the external
script or what? But an alert() gives it the time? I tried using
setTimtout with no lucky.
/* ajax(elmId, method, url)
* Ajax function with javascript on loaded page enabled
* with code stolen online
* jhlicc@{gmail,hotmail}.com, 20110626
*/
function ajax(elmId, method, url)
{
var xhr, elm;
xhr = new XMLHttpRequest();
elm = document.getElementById(elmId);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status == 200){
var s1, n1;
elm.innerHTML = xhr.responseText;
s1 = elm.getElementsByTagName("script");
n1 = s1.length;
for (var i = 0; i != n1; i++){
var s2;
s2 = document.createElement("script");
s2.type = "text/javascript";
if (s1.src) s2.src = s1.src;
if (s1.text) s2.text = s1.text;
elm.appendChild(s2);
if (s1.src){
/*Line#28*/ alert(src_appended);
}
}
for (var i = 0; i != n1; i++){
elm.removeChild(elm.getElementsByTagName("script")[0]);
}
} else {
elm.innerHTML = xhr.status + " " + xhr.statusText;
}}}
xhr.open(method, url);
xhr.send();
}
Thank you for your time