Javascript won't work in Firefox

L

LAshooter

I'm trying to use a javascript function that works in IE but not in Firefox.
Is there something about this code that is IE specific? If so, can I
add/alter something to make it work in more browsers?


Ex:<img src="thumbs/Dina.jpg" alt="Dina Marie Vannoni" width="39"
height="50" border="1" onClick="enlarge()"><script language="JavaScript">
function enlarge() {
oSrcElem = event.srcElement;
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}
</script>
Thanx,Wm(replies CC: to sdshooter at hotmail appreciated!)
 
R

RobG

LAshooter said:
I'm trying to use a javascript function that works in IE but not in Firefox.
Is there something about this code that is IE specific? If so, can I
add/alter something to make it work in more browsers?

Yes, and yes.
Ex:<img src="thumbs/Dina.jpg" alt="Dina Marie Vannoni" width="39"
height="50" border="1" onClick="enlarge()"><script language="JavaScript">

The language attribute is depreciated, type is required.
function enlarge() {
oSrcElem = event.srcElement;

Your use of 'event' here is the issue. IE uses a global 'event' that
is always available, Geko browsers don't. Depending on how you attach
your onclick, you may have to pass 'event' to the function. Then there
are a few things you need to do in the function to tidy-up.

... onClick="enlarge(event)">
<script type="text/javascript">
function enlarge(e) {
e = e || window.event; // fits both event models
var oSrcElem = e.target || e.srcElement; // fits both IE & others
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');

where does 'imgLarge' come from? If it's the id of an image element,
then it will work in IE 'cos it uses ids as global variables, but...

Other browsers don't. You could use the images collection but you'll
probably settle for getElementById.

You may want to look at the group FAQ for how to support older IE and
document.all:

}
</script>
Thanx,Wm(replies CC: to sdshooter at hotmail appreciated!)

What's said in the group stays in the group...
 
T

Thomas 'PointedEars' Lahn

Danny said:
Well, yes, srcElement is IE, mozilla uses .target, same object really,
now, you don't need it though, as you're using an event handler on the
actual element explicitly as opposed to binding it through its hierarchy,
so, instead, use:

function enlarge(el) {
el.src=el.src.replace(/thumbs/,'images')
}

onclick="enlarge(this)"

This is error-prone and does not degrade cleanly.

1. Having Thumbnails in a sibling directory is hardly good resource
management. Thumbnails should be located in a subdirectory or
(even better) in the same directory with slightly different
filenames.

2. What if a filename contains `thumbs'?

3. What if the replace() method or RegExp literals are not supported
accordingly?

4. What if the element is clicked twice?

5. What about users that don't have or don't want to use a pointing device?

6. What about users with client-side scripting disabled or just not
available?

Please stop top-posting, and please stop cross-posting without Followup-To.
<http://jibbering.com/faq/>


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 14 Jul
2005 18:53:58, seen in Thomas 'PointedEars'
Lahn said:
please stop cross-posting without Followup-To.
<http://jibbering.com/faq/>

Ignore the ramblings of this juvenile control freak. He has a Fuhrer-
complex.

I see nothing in the newsgroup FAQ about cross-posting of follow-ups,
and I know of no such requirement in general Usenet standards.

However, it is generally considered rude to set follow-up without
explicitly saying so in the body of the article.
 
R

Randy Webb

Thomas said:
Please stop top-posting, and please stop cross-posting without Followup-To.
<http://jibbering.com/faq/>

And this coming from someone who set the follow-up to a newsgroup that
is of questionable integrity/quality over a newsgroup that is chartered,
the *official* (J)ava/ECMAScript newsgroup, and proven to be of quality
and integrity?
WOW.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top