Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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

Reply With Quote
  #2 (permalink)  
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)
Reply With Quote
  #3 (permalink)  
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

Reply With Quote
  #4 (permalink)  
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

Reply With Quote
  #5 (permalink)  
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)
Reply With Quote
  #6 (permalink)  
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

Reply With Quote
  #7 (permalink)  
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

Reply With Quote
  #8 (permalink)  
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

Reply With Quote
  #9 (permalink)  
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)
Reply With Quote
  #10 (permalink)  
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:37 PM.


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