Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2005, 04:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default 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

 
Old January 7th, 2005, 05:11 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old January 7th, 2005, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old January 7th, 2005, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old January 7th, 2005, 08:21 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old January 7th, 2005, 11:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old January 7th, 2005, 11:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old January 7th, 2005, 11:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old January 7th, 2005, 11:30 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old January 7th, 2005, 11:35 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You have an extra quote before therapists:
Code:
'image_bin/'therapists_over.gif'
=>
Code:
'image_bin/therapists_over.gif'



--

Joe (Microsoft MVP - XML)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
How to load multiple Images on the Form? shah123 C# 1 May 20th, 2007 09:28 PM
how to load images vaneza VB How-To 3 August 2nd, 2005 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.