p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   pre load images error (http://p2p.wrox.com/showthread.php?t=23589)

Adam H-W January 7th, 2005 04:47 AM

pre load images error
 
Hi there

I'm getting this error message:

'Object doesn't support this property or method' when I'm trying to use a preload script for some rollovers.

this is the javascript:

preloadImg = function(imgObj,imgSrc) {
    if (document.images) {
        eval(imgObj + '=new Image()');
        eval(imgObj + '.src="' + imgSrc + '"');
    }
}

and later on in the page this is where I use it

<body onLoad="preloadImg('1','image_bin/1_over.gif','2','image_bin/2_over.gif','3','image_bin/'3_over.gif','4','image_bin/4_over.gif','5','image_bin/5_over.gif')">

not sure why it's happening - any help appreciated - thanks

Adam


joefawcett January 7th, 2005 05:11 AM

You have made a simple piece of code very cumbersome, why use:
Code:

preloadImage = function(...
Why not:
Code:

function preloadImg(...
Why use eval? NEVER use eval, each time you call it it starts a new script engine, I've only ever once seen the need for this function and that was in an app to teach JavaScript so that the app could run the student's dynamic code to see if it worked.

Why have
Code:

if (document.images)
before your script? You don't use the document.images collection so why test for it?

Your function also only has two arguments, imgObj and imgSrc, yet you are passing more than two parameters to it, thee others are ignored.

You are also trying to use variables such as 1, 2 and 3. Variables must start with a letter or underscore (I think this is the reason for the error message you are getting).

I would rewrite your code as:
Code:

function preloadImages(ImageSources)
{
  for (var i = 0; i < ImageSources.length; i++)
  {
    var oImage = new Image();
    oImage.src = ImageSources[i];
  }
}

and then pass an array of sources (note square brackets) to the function:
Code:

<body onLoad="preloadImages(['image_bin/1_over.gif', 'image_bin/2_over.gif', 'image_bin/'3_over.gif', 'image_bin/4_over.gif' ,'image_bin/5_over.gif'])">



--

Joe (Microsoft MVP - XML)

Adam H-W January 7th, 2005 06:49 AM

thanks Joe

I've re-written my code as such:

<script language="JavaScript" type="text/javascript">
function preloadImages(ImageSources)
{
  for (var i = 0; i < ImageSources.length; i++)
  {
    var oImage = new Image();
    oImage.src = ImageSources[i];
  }
}

</script>
<body onLoad="preloadImages(['image_bin/about_over.gif', 'image_bin/therapies_over.gif', 'image_bin/'therapists_over.gif', 'image_bin/find_over.gif' ,'image_bin/email_over.gif'])">

and now I get the error message: Object Expected

not sure why?

thanks

Adam


Adam H-W January 7th, 2005 08:15 AM

Hi Joe,

think I've found out why I get this error but unsure how to get round it: in my .js file I also use the new Image property for my rollovers and I think using it in the preload script too is confusing it. How would I get round this?

thanks
Adam


joefawcett January 7th, 2005 08:21 AM

Looks fine, are you sure that's exactly what you wrote, normally that error is caused by capitalisation discrepancies.
Can you debug the page and see exactly where the error occurs?


--

Joe (Microsoft MVP - XML)

Adam H-W January 7th, 2005 11:21 AM

Sure - I was trying to find the Microsoft debugger on my current machine but cannot find it - do you know if it comes with Windows XP; with my previous machine I had Windows 200 Pro?

thanks

Adam


Adam H-W January 7th, 2005 11:27 AM

Hi Joe

You can checkit out at http://80.82.139.234/st-woolsgrove/top.asp - I noticed that it seems to occur in IE but when I look at it in the Mozilla Firefox browser, it doesn't throw up an error.

thanks

Adam


Adam H-W January 7th, 2005 11:29 AM

sorry, try - http://80.82.139.234/st-woolsgrove/default.asp


joefawcett January 7th, 2005 11:30 AM

I think you can download the script debugger or you the one that comes with Office (it's an optional install). You need to uncheck "Disable script debugging" in the advanced section of IE => Tools => Internet Options.

I don't see why your code should conflict, maybe an error in the other code, can you post it?


--

Joe (Microsoft MVP - XML)

joefawcett January 7th, 2005 11:35 AM

You have an extra quote before therapists:
Code:

'image_bin/'therapists_over.gif'
=>
Code:

'image_bin/therapists_over.gif'



--

Joe (Microsoft MVP - XML)


All times are GMT -4. The time now is 07:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.