Forum Moderators: open
<html>
<head>
<script type='text/javascript' src='behaviors.js'></script>
</head>
<body>
<img id="Photo" src="" alt="" width="100" height="150" />
</body>
</html>
The file 'behaviors.js':
window.onload = function()
{
if (document.getElementById) {
var img = document.getElementById('Photo');
if (img) img.onload = InitFade;
}
}
function InitFade()
{
...
}
window.onload fires, the image will have already loaded. I personally can't see a way of doing this while guaranteeing that the image's
onload event hasn't already fired. The only scenario where I can see a dynamic onload event listener assignment working is where the image's src is defined (or changed) after the listener assignment.
This is clunky... but might work:
<html>
<head>
</head>
<body>
<img id="Photo" src="" alt="" width="100" height="150" />
<script type='text/javascript' src='behaviors.js'></script>
</body>
</html>
The file 'behaviors.js':
if (document.getElementById) {
var img = document.getElementById('Photo');
if (img) img.onload = InitFade;
}
function InitFade()
{
...
}