PDA

View Full Version : Image swap with JavaScript vs. IE7



RobertoShanghai
05-05-2007, 04:53 PM
1) Script Title:

showFilterButtons

2) Script URL :

www.roberto-colombo.com

3) Describe problem:

Hello,

I have been reported a bug on my script (working well with Netscape, IE5 etc.) with IE7.
A JS function simply change the image url, allowing to change the displayed image.
Something like:

if ( document.getElementById ) // IE5+ & Gecko
{
if ( document.getElementById( id ).innerHTML != null )
{
document.getElementById( id ).innerHTML.src = url;
}
}
else if ( document.getElementById ) // IE5+ & Gecko
{
if ( document.getElementById( id ) != null )
{
document.getElementById( id ).src = url;
}
}
else if (document.all) // IE4
{
if ( document.all[ id ] != null )
{
document.all[ id ].src = url;
}
}
else // Netscape 4
{
if ( document.images[ id ] != null )
{
document.images[ id ].src = url;
}
}

With IE7, this seems not working, no image is displayed. :-||
Of course, the "id" points to a valid image id, which is referenced in the HTML within the <img> tag.
Also, the same image id is created in JavaScript with the new Image() call.
In fact everything works fine with IE5, Netscape... did I miss anything regarding IE7 ?
Is there anythnig that should be changed ?
Can I still use document.getElementById in the same way ?

:-|

HELP... :confused:

techno_race
05-08-2007, 01:41 AM
Uhh...
I've noticed that IE7 throws almost all scripts out of wack.

mwinter
05-08-2007, 06:30 AM
if ( document.getElementById ) // IE5+ & Gecko
{
if ( document.getElementById( id ).innerHTML != null )
{
document.getElementById( id ).innerHTML.src = url;
}
}
else if ( document.getElementById ) // IE5+ & Gecko
{
if ( document.getElementById( id ) != null )
{
document.getElementById( id ).src = url;
}
}
else if (document.all) // IE4
{
if ( document.all[ id ] != null )
{
document.all[ id ].src = url;
}
}
else // Netscape 4
{
if ( document.images[ id ] != null )
{
document.images[ id ].src = url;
}
}

What on Earth is all that lot for? The images collection is (more-or-less) universal and preferred, not a legacy NN4 property:



if (document.images)
document.images[id].src = url;

Note that NN4 won't support this anyway as it has limited knowledge of the id attribute. If each img element has a name attribute with a value that matches the id attribute, then it would work.



With IE7, this seems not working, no image is displayed. :-||

I'm rather surprised that the first code branch works with anything, considering that the innerHTML property is a string, and strings have no src property.

Mike