homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
onload handler img
onload handler img
webaster




msg:1493555
 3:35 pm on Jan 24, 2005 (gmt 0)

<img id="Photo" onload="InitFade();" src="" alt="" width="100" height="150" />

How can I use onload for this image so that the onload is done not in the img tag

 

MikeFoster




msg:1493556
 8:37 pm on Jan 24, 2005 (gmt 0)

The xhtml file:

<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()
{
...
}

Bernard Marx




msg:1493557
 10:01 pm on Jan 24, 2005 (gmt 0)

Mike, I think you've missed something there. When
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.

MikeFoster




msg:1493558
 4:34 am on Jan 25, 2005 (gmt 0)

You're right, I missed it :)

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()
{
...
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved