PDA

View Full Version : Onload



Trinithis
06-16-2007, 03:56 AM
Is there a way to give tags that do not support the onload attribute (such as div) a working onload attribute? Or would I have to mimic it by placing a script tag right after the desired tag?

thetestingsite
06-16-2007, 04:06 AM
You would probably have to do something like this:



<div id="test">Some text</div>

<script type="text/javascript">
window.onload = document.getElementById('test').innerHTML = 'This is a test';
</script>


Not sure if this is the best way, but hope this helps.

Twey
06-16-2007, 02:28 PM
Is there a way to give tags that do not support the onload attribute (such as div) a working onload attribute? Or would I have to mimic it by placing a script tag right after the desired tag?You can poll it:
<script type="text/javascript">
function onExist(elID, func) {
var v = document.getElementById(elID);
if(!v)
setTimeout(function() { onExist(elID, func); }, 500);
else
func.call(v);
v = null;
}

onExist("test", function() {
alert(this.firstChild.nodeName);
});
</script>