472,789 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Script won't work in Netscape?

Wm
I have a script that changes a main/enlarged image when you click on a
thumbnail. It works fine in IE, but for some reason it won't do anything in
Netscape 7.1. Does anyone know if this is a problem with Netscape, or is
there something I can alter in this script that will allow it to work in
both IE and Netscape?
<script language="JavaScript">
function enlarge() {
oSrcElem = event.srcElement;
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}</script>
Example line: <img src="thumbs/JosielynI26.jpg" width="33" height="50"
border="1" onClick="enlarge()">Thanks for any/all assistance, and any
replies CC'd to my LAshooter HoTMaiL acct would be greatly appreciated!

Wm

Jul 23 '05 #1
4 1497


Wm wrote:
I have a script that changes a main/enlarged image when you click on a
thumbnail. It works fine in IE, but for some reason it won't do anything in
Netscape 7.1. Does anyone know if this is a problem with Netscape, or is
there something I can alter in this script that will allow it to work in
both IE and Netscape?
<script language="JavaScript">
function enlarge() {
oSrcElem = event.srcElement;
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');
}</script>
Example line: <img src="thumbs/JosielynI26.jpg" width="33" height="50"
border="1" onClick="enlarge()">


You want
<script type="text/javascript">
function enlarge (img) {
document.images.imgLarge.src =
img.src.replace(/thumbs/,'images');
}
</script>
with
<img name="imgLarge" ...>
and
<img src="thumbs/JosielynI26.jpg" onclick="enlarge(this);" ...>

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
[CC'd to LA*******@hotmail.com]

On Mon, 26 Jul 2004 03:27:27 GMT, Wm <lu********@comcast.net> wrote:
I have a script that changes a main/enlarged image when you click on a
thumbnail. It works fine in IE, but for some reason it won't doanything
in Netscape 7.1. Does anyone know if this is a problem withNetscape, or
is there something I can alter in this script that willallow it to work
in both IE and Netscape?
The problem is not with Netscape; your script is written for IE (and its
imitators).
<script language="JavaScript">
The type attribute is required. That line should read:

<script type="text/javascript">

The language attribute is deprecated: don't use it.
function enlarge() {
oSrcElem = event.srcElement;
Netscape and other, similar, browsers do not use a global event object.
The event object is passed as an argument to the event. The usual way
attend to both conventions is to use:

function enlarge(evt) {
var obj = null; // *Always* declare local variables with the var
// keyword. Omitting it will make the variable
// global (bad practice).
evt = evt || event; // If evt evaluates to false (null or undefined,
// in this case), assign a reference to the
// global event object.

We now come to the next problem: Microsoft do not follow the Document
Object Model (DOM) Events specification, and use different properties to
virtually everyone else. Browsers that follow Microsoft use
Event.srcElement, and everyone else uses Event.target.

if (evt) {
if (evt.target) {
obj = evt.target;
} else if (evt.srcElement) {
obj = evt.srcElement;
}

Now, finally, you can change the image source. However, to do that, we
must remove one final Microsoft-ism.
imgLarge.src = oSrcElem.src.replace(/thumbs/,'images');


IE allows you to refer to many different types of objects in many varying
ways, the worst of which is displayed above. imgLarge is presumably the id
or name of an IMG element, and refering to this name in IE will yield a
reference to the object: not so in other browsers!

If the event was fired by a separate element (not the image), the best way
to refer to the image is through the images collection, which is supported
as far back as Netscape 4:

if (obj) {
document.images['imgLarge'].src =
obj.src.replace(/thumbs/, 'images');
}
}
}
</script>

To complete the changes, you need to change the function call in order to
pass the event object[1]:

<img id="imgLarge" src="thumbs/image.jpg" ... onclick="enlarge(event)">

But, you do fire the event from the image. This allows a drastic reduction
to the code:

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

And the appropriate call change:

<img src="thumbs/image.jpg" ... onclick="enlarge(this)">

Inside an intrinsic event listener, 'this' refers to the object to which
the event is targeted. I also removed the id attribute as it's now
redundant (unless, of course, you use it elsewhere.

I imagine that you will have to remove the width and height attributes
from the IMG element, because they will not change when the image is
replaced: you'll end up with a full-sized image squashed into a small
rectangle.

Hope that helps,
Mike

[1] (For completeness) I know I said that the event attribute is passed to
the function, so why did I still include the identifier, event? When the
page is parsed, the value of the intrinsic event attributes (which, in
reality, is just text) is inserted into an anonymous function assigned to
the event listener:

document.images['imgLarge'].onclick = function(event) {
enlarge(event);
};

The function, as shown above, has a single argument, event, which holds
the event object in browsers such as Mozilla and Netscape. In the HTML a
few paragraphs above, it this argument that the identifier, event, is
referring to.

The same, overall effect could be achieved by placing the following code
in a SCRIPT element after the IMG:

document.images['imgLarge'].onclick = enlarge;

When called by the browser, the function, enlarge, will be passed a
reference to the event object, just like the anonymous function.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #3
Hi WM,

I think you should buy Michael a couple of beers now. ;-)

Regards,
Erwin Moller
Jul 23 '05 #4
On Mon, 26 Jul 2004 16:11:42 +0200, Erwin Moller wrote:
I think you should buy Michael a couple of beers now. ;-)


Wow, I feel like I own 'im a couple, too.

Things that hadn't worked for me and the ugly hacks I'd resorted to,
all make more sense now.

Thanks!
--
sls
Jul 23 '05 #5

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

Similar topics

1
by: Benoit Fries | last post by:
Hello, I have some problems with a Javascript that should redirect me to another page, it works fine most of the times but sometimes it hangs on a blank page. I made lots of tests and finally...
2
by: Matthew | last post by:
Is there a javascript or alternative default fill friendly way for counting down the remaining characters left in a form box?
3
by: Gord | last post by:
http://eastontario.com/promo/tableCellMultiplier.htm For a rectangular/square n column, m row table, this script formats so that all cells hold the same contents. Check out the source and vary...
2
by: tmb | last post by:
- Want to open a 2nd browser window when someone clinks a link with the link content inside it. - Want to control window size - Want all tool bars so only blue bar at top shows - Can I put my...
9
by: ALuPin | last post by:
Hi newsgroup users, I have the following java-script: </SCRIPT> </head> <body text='' link='' vlink='' alink='' bgcolor='FFFFFF'> <p> <center><TABLE cellSpacing=1 cellPadding=1...
8
by: Arun Seetharam | last post by:
Dear Folks, The application I am working on uses Frames (TopFrame, SideFrame(Left) and the MainFrame). On the occurance of a certain event, I am closing the window from one of the frames. This...
5
by: zaw | last post by:
Hi I am working on implementing this script to shopping cart. Basically, it copies fill the shipping address from billing automatically. I believe one or more syntax is not netscape compatible....
7
by: Russ | last post by:
Hi All, I have a problem getting the following simple example of "document.write" creating a script on the fly to work in all html browsers. It works in I.E., Firefox, and Netscape 7 above. It...
3
by: niconedz | last post by:
Hi The following code works fine in IE but not Firefox. It's a little script that zooms an image and resizes the window to fit. Can anybody tell me what's wrong? Thanks Nico == btw.....
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.