Connecting Tech Pros Worldwide Forums | Help | Site Map

document.images[imgName] has no properties

Jeff
Guest
 
Posts: n/a
#1: Nov 23 '05
I get the following error when I try to rollover my button:

document.images[imgName] has no properties Line: 22

Am I doing this correctly? Suggestions? This is using Netscape 7.2
on Fedora core 4.

Here's my simple html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<script type="text/javascript">
<!-- HIDE

if (document.images)
{
document.images["order1"] = new Image(135,40);
document.images["order1"].src = "images/ordernow1.jpg";;
document.images["order2"] = new Image(135,40);
document.images["order2"].src = "images/ordernow2.jpg";;
}

function rollover(imgName,src)
{
if (document.images)
{
document.images[imgName].src = src;
return false;
}
return true;
}

// DONE hiding -->
</script>

</head>

<body>

<a href="http://www.mysite.com/order.html"
onMouseover="return rollover('order2','images/ordernow2.jpg');"
onMouseout="return rollover('order1','images/ordernow1.jpg');">
<img src="images/ordernow1.jpg">
</a>

</body>
</html>


VK
Guest
 
Posts: n/a
#2: Nov 23 '05

re: document.images[imgName] has no properties



Jeff wrote:[color=blue]
> I get the following error when I try to rollover my button:
>
> document.images[imgName] has no properties Line: 22[/color]

Change:
function rollover(imgName,src)
{
if (document.images)
{
document.images[imgName].src = src;
return false;
}
return true;
}

to

function rollover(imgName,neverUseNativeNameAsVarName)
{
if (document.images)
{
document.images[imgName].src = neverUseNativeNameAsVarName;
return false;
}
return true;
}

;-)

Jeff
Guest
 
Posts: n/a
#3: Nov 23 '05

re: document.images[imgName] has no properties


Ok, that was a dumb mistake on my part :) However,
changing the 'src' variable to anything else still doesnt
work. I still get the same error.

Just a note on my orig code, IE doesnt complain at all.
Im using IE 6.0. Then again it doesnt rollover either, but
no errors.

Thanks for the first part, try again? :)

Michael Winter
Guest
 
Posts: n/a
#4: Nov 23 '05

re: document.images[imgName] has no properties


On 21/11/2005 19:17, Jeff wrote:

[snip]
[color=blue]
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">[/color]

Unless you /need/ a Transitional feature, use a Strict document type
declaration.

[snip]
[color=blue]
> <script type="text/javascript">
> <!-- HIDE[/color]

Hide from what? Browsers that are old enough not to recognise a SCRIPT
element (even if they don't support client-side scripts) aren't used on
the Web, not least because they're all but useless now.
[color=blue]
> if (document.images)
> {
> document.images["order1"] = new Image(135,40);[/color]

The images collection should be used to /retrieve/ images already in the
document. Create a new object to retain image objects for preloading
purposes:

var preload = {};

/* Check for support of the Image constructor */
if(('object' == typeof Image) || ('function' == typeof Image)) {
/* Preload */
preload.order1 = new Image(135, 40);
preload.order1.src = 'images/ordernow1.jpg';

/* ... */
}

[snip]
[color=blue]
> <a href="http://www.mysite.com/order.html"
> onMouseover="return rollover('order2','images/ordernow2.jpg');"
> onMouseout="return rollover('order1','images/ordernow1.jpg');">[/color]

Your rollover function will attempt to look for IMG elements with a name
or id attribute value of 'order1' and 'order2' (depending on the event),
but no such element exists. This is why your code fails.
[color=blue]
> <img src="images/ordernow1.jpg">[/color]

This element should have an alt attribute (required, even if an empty
string), and must be identified so that the script can reference it.
Change the strings in the event listeners above to reflect this name:

<a href="..."
onmouseover="rollover('order', 'images/ordernow2.jpg');"
onmouseout="rollover('order', 'images/ordernow1.jpg');">
<img id="order" name="order"
alt="..."
src="images/ordernow1.jpg">
</a>

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Michael Winter
Guest
 
Posts: n/a
#5: Nov 23 '05

re: document.images[imgName] has no properties


On 21/11/2005 21:11, Jeff wrote:
[color=blue]
> Ok, that was a dumb mistake on my part :)[/color]

No, it wasn't. You can use the identifier, src, if you want.

[snip]

Mike


Please quote when you're replying to a post. You can do this with Google
Groups by opening the options panel (show options) at the top of the
post you want to reply to and select 'Reply' from there.

Trim away text that is irrelevant to your reply (preferably indicating
that you've done so), and interleave your comments.

See <http://www.jibbering.com/faq/faq_notes/clj_posts.html>,
particularly the section on 'Interleaved Posting...'.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Randy Webb
Guest
 
Posts: n/a
#6: Nov 23 '05

re: document.images[imgName] has no properties


Jeff said the following on 11/21/2005 2:17 PM:[color=blue]
> I get the following error when I try to rollover my button:
>
> document.images[imgName] has no properties Line: 22
>
> Am I doing this correctly? Suggestions? This is using Netscape 7.2
> on Fedora core 4.
>
> Here's my simple html file:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
>
> <head>
>
> <script type="text/javascript">
> <!-- HIDE
>
> if (document.images)
> {
> document.images["order1"] = new Image(135,40);
> document.images["order1"].src = "images/ordernow1.jpg";;
> document.images["order2"] = new Image(135,40);
> document.images["order2"].src = "images/ordernow2.jpg";;
> }
>
> function rollover(imgName,src)
> {
> if (document.images)
> {
> document.images[imgName].src = src;
> return false;
> }
> return true;
> }
>
> // DONE hiding -->
> </script>
>
> </head>
>
> <body>
>
> <a href="http://www.mysite.com/order.html"
> onMouseover="return rollover('order2','images/ordernow2.jpg');"
> onMouseout="return rollover('order1','images/ordernow1.jpg');">
> <img src="images/ordernow1.jpg">[/color]

Ignore VK and his ramblings, we are still trying to educate him. Give
your img tag a NAME attribute:

<img src="something.." name="myImage">

document.images['myImage'].src="....."

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jeff
Guest
 
Posts: n/a
#7: Nov 23 '05

re: document.images[imgName] has no properties


Thanks to all for teaching me some things I didnt know.
I did what Randy said and it worked just fine.

Thanks!

Michael Winter
Guest
 
Posts: n/a
#8: Nov 23 '05

re: document.images[imgName] has no properties


On 21/11/2005 21:32, Michael Winter wrote:

[snip]
[color=blue]
> Hide from what? Browsers that are old enough not to recognise a SCRIPT
> element (even if they don't support client-side scripts) aren't used on
> the Web, not least because they're all but useless now.[/color]

Hmm. That didn't come out quite right.

All browsers currently in use on the Web recognise SCRIPT elements, even
if they don't support client-side scripts. They won't render the
contents of the element. If there is some other reason to 'hide' a
script, the better solution would be to place it in an external file.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jasen Betts
Guest
 
Posts: n/a
#9: Nov 23 '05

re: document.images[imgName] has no properties


On 2005-11-21, Jeff <jeep@rahul.net> wrote:[color=blue]
> I get the following error when I try to rollover my button:[/color]
[color=blue]
>
> Here's my simple html file:[/color]
[color=blue]
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"[/color]
[color=blue]
> document.images["order1"] = new Image(135,40);>[/color]

this image doesn't appear to be part of your dcument.
where does the name "order1" come frem - what are you doing to ensure that
this new image keeps that name.

[color=blue]
> onMouseover="return rollover('order2','images/ordernow2.jpg');"
> onMouseout="return rollover('order1','images/ordernow1.jpg');">[/color]
^
|
|
for HTML 4.01 Transitional this should be lower case IIRC
[color=blue]
><img src="images/ordernow1.jpg">[/color]

nope, that desn't match it....



--

Bye.
Jasen
Randy Webb
Guest
 
Posts: n/a
#10: Nov 23 '05

re: document.images[imgName] has no properties


Jasen Betts said the following on 11/22/2005 2:48 PM:[color=blue]
> On 2005-11-21, Jeff <jeep@rahul.net> wrote:
>[color=green]
>>I get the following error when I try to rollover my button:[/color]
>
>[color=green]
>>Here's my simple html file:[/color]
>
>[color=green]
>><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"[/color]
>
>[color=green]
>> document.images["order1"] = new Image(135,40);>[/color]
>
>
> this image doesn't appear to be part of your dcument.
> where does the name "order1" come frem - what are you doing to ensure that
> this new image keeps that name.[/color]

And it doesn't matter. It is just a preloading routine.
[color=blue]
>
>[color=green]
>> onMouseover="return rollover('order2','images/ordernow2.jpg');"
>> onMouseout="return rollover('order1','images/ordernow1.jpg');">[/color]
>
> ^
> |
> |
> for HTML 4.01 Transitional this should be lower case IIRC[/color]

No, it doesn't matter. Not in HTML4.01 anyway.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread