Click to See Complete Forum and Search --> : After Load Hack


redijedi
04-24-2004, 09:22 PM
I know that there is no api that will wait until something has finished loading on a page, but does anyone know of any hacks to get this working?

I'm trying to reference an html element in an included js file. However the element hasn't been loaded when the functions try to access it. I know I can move the included script to the bottom of the calling html file, but this seems like a dirty way of doing it. Besides, my page is created dynamically and is based off a template that will only insert data in the head element.

Any ideas?

IceMetalPunk
04-24-2004, 09:32 PM
You could put a setTimeout on it, so that it goes into action after a certyain amount of time, which should be enough time for it to load.

-IceMetalPunk ;)

crh3675
04-24-2004, 10:09 PM
Use setInterval:



var timer;

function pageLoad(){
timer=setInterval("checkElement()",100);
}

function checkElement(){
if(document.getElementByID("yourelement")){
clearInterval(timer);
// do some code here because it exists now
}
}

redijedi
04-24-2004, 10:19 PM
Okay. I wrote in the time functions, however much to my disappointment, it seems that you cannot reference instance variables. Can this be right?

After seeing that it cannot reference instance variables, I tried to pass a parameter into the function I call in settimeout or setinterval. That didn't work either.

Am I correct, or an idiot?

redijedi
04-24-2004, 10:23 PM
Maybe some code would help explain:



In constructor:

this.timer = setInterval("loadCheck()",1000);


In loadCheck:

// If the table handler doesn't error out, clear the timer
if (_setTableHandler()) {
clearInterval(this.timer);
}


In _setTableHandler:

// Set the handle of the actual table element based on the table id
this.itblDynamic = document.getElementById(this.istrName);

// If the element have not been loaded, produce an error
if (this.itblDynamic == null) {
return alert ("Error: Not able to get table element referenced by " + this.istrName + ".");
}

// If the specified element is not a table element produce an error
if (this.itblDynamic.tagName.toLowerCase() != "table") {
return alert ("Error: Not able to control element " + this.itblDynamic.tagName + ".");
}

return 1;



The called functions cannot seem to read the this.istrName var, even though I can read it from the constructor.