473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Image swap only works in IE

I have a portfolio page which loads a dozen thumbnails and one large image.
A friend helped me code a script (below) which will swap out the large image
(named "imgLarg") when a different thumbnail is clicked. Both the thumbnail
and the enlargement are identically named, one is in /thumbs/ and one is in
/enlargements/ - tricky, huh? ;-) What's the easiest way to make this work
in other browsers as well?

<script language="JavaScript">
function enlarge() {
oSrcElem = event.srcElement;
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}
</script>
Thanks,
Wm

Mar 5 '06 #1
12 1620
LAshooter wrote:
I have a portfolio page which loads a dozen thumbnails and one large
image. A friend helped me code a script (below) which will swap out
the large image (named "imgLarg") when a different thumbnail is
clicked. Both the thumbnail and the enlargement are identically named,
one is in /thumbs/ and one is in /enlargements/ - tricky, huh? ;-)
What's the easiest way to make this work in other browsers as well?


Not to use it at all, because it is junk. Compare

<URL:http://pointedears.de/scripts/test/hoverMe>
PointedEars
Mar 5 '06 #2
The script works very well in IE, just not in other browsers. Your reference
displays a black screen with nothing but an "About" graphic at the top. Is
something supposed to work there???

Wm
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:33****************@PointedEars.de...
LAshooter wrote:
I have a portfolio page which loads a dozen thumbnails and one large
image. A friend helped me code a script (below) which will swap out
the large image (named "imgLarg") when a different thumbnail is
clicked. Both the thumbnail and the enlargement are identically named,
one is in /thumbs/ and one is in /enlargements/ - tricky, huh? ;-)
What's the easiest way to make this work in other browsers as well?


Not to use it at all, because it is junk. Compare

<URL:http://pointedears.de/scripts/test/hoverMe>
PointedEars

Mar 5 '06 #3
LAshooter wrote:
I have a portfolio page which loads a dozen thumbnails and one large image.
A friend helped me code a script (below) which will swap out the large image
(named "imgLarg") when a different thumbnail is clicked. Both the thumbnail
and the enlargement are identically named, one is in /thumbs/ and one is in
/enlargements/ - tricky, huh? ;-) What's the easiest way to make this work
in other browsers as well?

<script language="JavaScript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function enlarge() {
oSrcElem = event.srcElement;
Your use of 'event' (i.e. the event object) here is one factor that makes
your script IE-centric. There are two basic event models: W3C and IE [1].
But there is no need to use the event object at all - you can pass a
reference directly from the element clicked on using 'this'.

On your thumbnail, use an onclick attribute like:

<img onclick="enlarge(this);" ... >
then:

function enlarge (oSrcElem){
// ...
Now oSrcElem is a reference to the element that fired the click event
without using the event object. Using 'this' is much more reliable and
consistent across various browsers.

imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
Here 'imgLarge' is either the name or id of the large image element, right?
And there you have your second IE-centric bit of code. IE makes all
element names and ID properties of the global object, a rather silly idea
that causes all sorts of problems.

In other browsers, you can get a reference to a named image using the
images collection (part of DOM 0), so in Firefox:

document.images.imgLarge
is a reference to the the image element with a name or ID of 'imgLarge'.
For some unknown reason, IE doesn't support that, you have to use the index
number. So if imgLarge is the 5th image in the document:

document.images[4]
would do the trick (and would work in Firefox too).

Anyhow, you are probably best to use getElementById with fall back to
document.all if you want to support IE 4. Try the following:

function enlarge (oSrcElem)
{

var imgLarge;

if (document.getElementById){
imgLarge = document.getElementById('imgLarge');
} else if (document.all){
imgLarge = document.all['imgLarge'];
}
if (imgLarge){
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}

}
</script>

While compiling the above, it occurred to me that it may be OK to use:

function enlarge (oSrcElem)
{
if (!imgLarge && document.getElementById){
var imgLarge = document.getElementById('imgLarge');
}
if (imgLarge){
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}
}
Untested. Of course anyone without scripting will see nothing happen.

1. Read about the different event models here:

<URL:http://www.quirksmode.org/js/introevents.html>
--
Rob
Mar 5 '06 #4
RobG wrote:
<snip>
document.images.imgLarge
is a reference to the the image element with a name or ID
of 'imgLarge'. For some unknown reason, IE doesn't support
that, you have to use the index number.
Are you certain of that. I have never seen IE fail to resolve a named
image as a property of the - document.images - object?

<snip> While compiling the above, it occurred to me that it may be
OK to use:

function enlarge (oSrcElem)
{
if (!imgLarge && document.getElementById){
var imgLarge = document.getElementById('imgLarge');
}
if (imgLarge){
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}
}
Untested.


It won't behave as implied, just as coded. The - var imgLarge - is not
conditional, function local variables are created prior to the execution
of any function body code so when the unqualified Identifier -
imgLarge - is first subject to the NOT operator it will resolve as a
local variable that has not yet been assigned a value, so - undefined -,
type convert to false and evaluate the NOT as true, always. And as the
result of - !imgLarge - is always true there is no point in carrying out
that test at all.

Richard.
Mar 5 '06 #5
"LAshooter" <la*******@hotmail.com> writes:
I have a portfolio page which loads a dozen thumbnails and one large image.
A friend helped me code a script (below) which will swap out the large image
(named "imgLarg") when a different thumbnail is clicked. Both the thumbnail
and the enlargement are identically named, one is in /thumbs/ and one is in
/enlargements/ - tricky, huh? ;-)
Not particularly. Your code suggests that the directories are actually
"thumbs" and "images". Which is it? I'll assume "enlargements" for now.
What's the easiest way to make this work in other browsers as well?
Rewrite from scratch. The script assumes that it runs in IE, and does
things that only work in the browser.
<script language="JavaScript">
For completeness, this is better (more standard compliant and works
just as well) written as:
<script type="text/javascript">
function enlarge() {
When you want to work on the element that was clicked, the easiest
way to do that is to receive the element as an argument. It is
easy to provide, since it is available in the onclick event handler:
<img src="../thumbs/..." onclick="enlarge(this);">
Then receive it as:
function enlarge(thumbImage) {
oSrcElem = event.srcElement;
That makes this IE'ism unnecessary.
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
Here you need to get a reference to the image element with name
"imgLarge". That is best done through the images collection:

document.images['imgLarge']

The assignment looks fine, as long as you are sure "thumbs" cannot
occour elsewhere in the string, previous to the instance to replace.

So:
document.images['imgLarge'].src =
thumbImage.src.replace(/thumbs/,"enlargements");
}
</script>

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 5 '06 #6
Richard Cornford wrote:
RobG wrote:
<snip>
document.images.imgLarge
is a reference to the the image element with a name or ID
of 'imgLarge'. For some unknown reason, IE doesn't support
that, you have to use the index number.

Are you certain of that. I have never seen IE fail to resolve a named
image as a property of the - document.images - object?


I /was/, but testing shows me to be wrong. I seem to remember that IE
didn't let you do it, I'm trying to track down where I got that idea from.

<snip>
While compiling the above, it occurred to me that it may be
OK to use:

function enlarge (oSrcElem)
{
if (!imgLarge && document.getElementById){
var imgLarge = document.getElementById('imgLarge');
}
if (imgLarge){
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}
}
Untested.

It won't behave as implied, just as coded. The - var imgLarge - is not
conditional, function local variables are created prior to the execution
of any function body code so when the unqualified Identifier -
imgLarge - is first subject to the NOT operator it will resolve as a
local variable that has not yet been assigned a value, so - undefined -,
type convert to false and evaluate the NOT as true, always. And as the
result of - !imgLarge - is always true there is no point in carrying out
that test at all.


Thanks for the explanation. Changing the first line to:

if (! window.imgLarge && document.getElementById){
gets it to work. I don't think I'd ever use it in real life though.
--
Rob
Mar 5 '06 #7
On 05/03/2006 12:57, RobG wrote:
Richard Cornford wrote:


[snip]
Are you certain of that. I have never seen IE fail to resolve a
named image as a property of the - document.images - object?


I /was/, but testing shows me to be wrong. I seem to remember that IE
didn't let you do it, I'm trying to track down where I got that idea
from.


You're probably confusing the images collection with the links
collection. The latter can only be indexed by ordinal number in IE.
However, with IE 6, one can use the namedItem method to obtain
references to source anchors (<a href="..."), though the equivalent dot-
or bracket-notation will still fail.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 5 '06 #8
LAshooter wrote:
The script works very well in IE, just not in other browsers. Your
reference displays a black screen with nothing but an "About" graphic
at the top. Is something supposed to work there???
Yes, there is a hover effect on that graphic, facilitated with a far better
"Image swap" code as you presented. If you would have cared to read (the
source code), you would have known.
[Top post]


Score adjusted.
PointedEars
Mar 5 '06 #9
I did look at the source code, and thought it was a helluva lotta code for
what looks like a simple mouseover. But thanks for all your help anyways.
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:29****************@PointedEars.de...
LAshooter wrote:
The script works very well in IE, just not in other browsers. Your
reference displays a black screen with nothing but an "About" graphic
at the top. Is something supposed to work there???


Yes, there is a hover effect on that graphic, facilitated with a far
better
"Image swap" code as you presented. If you would have cared to read (the
source code), you would have known.
[Top post]


Score adjusted.
PointedEars

Mar 5 '06 #10
Here is a definitive answer for doing your image swap for all browsers.
The trick is to attach your event handler in such a way that it passes
the Event Object to your function (which applies only if your using
Netscape/FireFox) and to design your function so that it uses either
window.event (for IE) or the passed in Event Object (NS/FF).

<script language=javascript>
function enlarge(e) {
// Normalize the event object into standard Object regardless of
Browser
var _event=(window.event)? event : e;
// Obtain handle to actual IMG Object that triggered the event
var theImg = (_event.srcElement) ? _event.srcElement :
_event.currentTarget;
// Swap out the source path from the "thumbs" to the "enlargements"
directory
theImg.src = theImg.src.replace(/thumbs/i,'enlargements');
}
</script>

<img src="http://www.abc.com/images/thumbs/blahblah.gif"
onclick="enlarge(event)">

Shannon Norrell

Mar 6 '06 #11
webdood wrote:
Here is a definitive answer for doing your image swap for all browsers.
No, it's not for the reasons listed in at least two previous posts.

The trick is to attach your event handler in such a way that it passes
the Event Object to your function (which applies only if your using
Netscape/FireFox) and to design your function so that it uses either
window.event (for IE) or the passed in Event Object (NS/FF).
There is no need to use the event object, 'this' provides a
cross-browser solution without any code forking and much less code.

<script language=javascript>
The language attribute is deprecated, type is required.

[...]
// Swap out the source path from the "thumbs" to the "enlargements"
directory
theImg.src = theImg.src.replace(/thumbs/i,'enlargements');


'theImg' refers to what? Where was it defined? This relys on IE's
trick of turning names and IDs into global properties.

This solution is not suitable for a great many browsers and absolutely
not 'definitive'.

[...]
--
Rob
Mar 6 '06 #12
LAshooter wrote:
I did look at the source code, and thought it was a helluva lotta code
for what looks like a simple mouseover. [...]
Reliable, efficient, simple. Pick two.
[Top post]


Last call for flight
<URL:http://www.safalra.com/special/googlegroupsreply/>, gate 80,
ladies and gentlemen!
PointedEars
Mar 6 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
I am rotating images of different dimensions. My problem is that when a new image is displayed in a new position which had an image of a different dimension, the old image is first stretched to the...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
4
by: bridgemanusa | last post by:
Hi All: I have a very long page of html that I want to take portions and hide them in divs, then show when a link is clicked. I have the hide show part working when the link is clicked, however...
2
by: Barkster | last post by:
I have a image that I want to click and when clicked that image will be swapped and then show a hidden div. When another image like it is clicked it will swap image and hide that text and so on. ...
7
by: KiwiBrian | last post by:
On a web page I have a graphic for a Shopping Cart Submit button. It is within a form and working fine. I would like to perform an image swap to an identical graphic of a different colour when I...
3
by: seamlyne | last post by:
The first method I ever used for multiple state buttons was to create a graphic for each button for each state: AboutUs_on, AbooutUs_over, AboutUs_out, etc. That works great when there are just a...
3
by: steve.peticca | last post by:
Hi, My onmousevent fails to switch the large photo with one of the smaller images. To be frank, I'm extremely new to Javascript programming and as such, am trying to learn from other sites...
3
by: PEJO | last post by:
I'm not much of a JS programmer so I used the the standard Macromedia swap image function for a disjointed rollover..which works fine.. no problem swapping the image with that code.... but when...
14
by: =?Utf-8?B?Sm9lIFRob21wc29u?= | last post by:
I have a Windows form program written in C# 2005. I continually read a stream of data via TCP that comes from a video camera (NTSC). When I have all the data for one frame I update a picturebox...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.