Jump to content

how to know when the script is loaded?


mysterbx

Recommended Posts

Hello,

 

im using the script from http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

 

and i cant figure how to know if my script (its below) is loaded...

i started using javascripts only a week ago, and im not so good at it, so please help me out!

 

function loadit() {
ajaxpage('test1.htm', 'contentarea1');
ajaxpage('test2.htm', 'contentarea2)';
ajaxpage('test3.htm', 'contentarea3');
}

 

so ho dow i know when "loadit" function is finished the work?

 

its the last component to my search engine ;)

Link to comment
https://forums.phpfreaks.com/topic/117639-how-to-know-when-the-script-is-loaded/
Share on other sites

Your ajaxpage function, does it had callback function when the ajax returned the value. Like:

 

if(httpRequest.readyState == 4) {
    // Done~
}

 

If it does, it might look something like:

function loadit() {
ajaxpage('test1.html', 'contentarea1', callback);
ajaxpage('test2.html', 'contentarea2', callback);
ajaxpage('test3.html', 'contentarea3', callback);
}

var readyPage = 0;
function callback() {
    readyPage ++;

    if(readyPage >= 4) {
        // Here you know your function is totally completely run
    }
}

 

There is no direct code for this purpose. You had to use event handler to capture it.

yes, ti has the function

if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {

 

but still nothing shows up (the should be an alert)

 

here is my full test page source:

<script type='text/javascript' src='images/b0.js'></script>
<script>
function loadit() {
mysterbx('2.php', 'x', callback);
mysterbx('2.php', 'z', callback);
mysterbx('2.php', 'c', callback);
}

var readyPage = 0;
function callback() {
    readyPage ++;

    if(readyPage >= 4) {
alert('done');
    }
}
</script>
<div id='x'></div>
<div id='z'></div>
<div id='c'></div>

 

any ideas?

i changed the code, but still the same "nothing", i think the main script (ajax script) needs correction

 

i will post the full page source + the main script, maybe it will be easier?

 

2.php:

<script type='text/javascript' src='b0.js'></script>
<script>
function loadit() {
mysterbx('2.php', 'x', callback);
mysterbx('2.php', 'z', callback);
mysterbx('2.php', 'c', callback);
}

var readyPage = 0;
function callback() {
    readyPage ++;

if(readyPage >= 3) {
    alert('done');
}
}
</script>
<div id='x'></div>
<div id='z'></div>
<div id='c'></div>

 

b0.js:

var bustcachevar=1
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""
function mysterbx(url, containerid, xload, xtype){
var page_request = false
if(!xload) {
loadingtext="";
} else {
loadingtext=xload;
}
xxtype=xtype;
if (window.XMLHttpRequest)
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar)
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}
function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
document.getElementById(containerid).innerHTML=page_request.responseText; return false;
} else {
document.getElementById(containerid).innerHTML = loadingtext; return false;
}
}

 

and thanks a lot for helping, this is the last component of page, now its a little  buggy, if everything works out fine, then a huge bug will be fixed, many users unhappy for it ;)

i just saw this:

function mysterbx(url, containerid, xload, xtype){

 

thats why the script doesnt work, there are 4 values, the 5th should be "callback"

 

i did this:

function loadit() {
mysterbx('2.php', 'x','','', callback);
mysterbx('2.php', 'z','','', callback);
mysterbx('2.php', 'c','','', callback);
}

 

.. but still nothing! :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.