A JS script which works fine in IE / FF, but not in Opera... (?)

F

Fox

Hi there!

I'm new on the group :)
I recently started to learn JavaScript, and I have a really weird
problem...

The script below (a simple slideshow) is working okay in both IE and
FireFox, but in Opera it displays only the first image.... I would
like to make it working on any browsers, I already tried many
variations, but it is still not working in Opera (I am using Opera
9.23 build 660 on Linux).

<HTML>
<HEAD>
</HEAD>
<BODY>

<DIV NAME=Photo>
<IMG SRC=snap1.png BORDER=1 WIDTH=512 HEIGHT=448>
</DIV>

<script>
i=2
function NextImg(){
if(i>2)i=1
document.all["Photo"].innerHTML="<IMG SRC=snap"+i+".png
BORDER=1 WIDTH=512 HEIGHT=448>"
i++
}
setInterval("NextImg()",4000)
</script>

</BODY>
</HTML>

Have anybody already seen such trouble with Opera?

Best regards,
Mateusz Viste
 
G

Gregor Kofler

Fox meinte:
The script below (a simple slideshow) is working okay in both IE and
FireFox, but in Opera it displays only the first image....

How about having a look at the JS error console? [1]
I would
like to make it working on any browsers, I already tried many
variations, but it is still not working in Opera (I am using Opera
9.23 build 660 on Linux).

Interesting, that it works on *any* browser...
<HTML>
<HEAD>
</HEAD>
<BODY>

<DIV NAME=Photo>
<IMG SRC=snap1.png BORDER=1 WIDTH=512 HEIGHT=448>
</DIV>

Enclosing attribute values in quotes is never a bad idea...
Divs don't have names, maybe IDs.

Type missing.
i=2
function NextImg(){
if(i>2)i=1
document.all["Photo"].innerHTML="<IMG SRC=snap"+i+".png

document.all is proprietary, can be easily replaced by getElementById(),
sometimes getElementsByName(), etc.

Why innerHTML? Exchanging the src value would do.
BORDER=1 WIDTH=512 HEIGHT=448>"
i++
}
setInterval("NextImg()",4000)

setInterval() is a method of the window object.
</script>

</BODY>
</HTML>

Have anybody already seen such trouble with Opera?

Plenty of opportunities for any browser to get into problems.

And when you're at it: Make your code more readable; some extra
whitespace sprinkled here and there, semicolons for terminating
commands, ...

Gregor


[1] http://operawiki.info/OperaJSConsole
 
F

Fox

How about having a look at the JS error console? [1]

Good Idea :)
document.all is proprietary, can be easily replaced by getElementById(),
sometimes getElementsByName(), etc.

Why innerHTML? Exchanging the src value would do.

Interesting!
Indeed, it seems that the line above is making trouble to Opera. How
to replace document.all to GetElementById? What's wrong in innerHTML?

Thank you!
 
G

Gregor Kofler

Fox meinte:
Indeed, it seems that the line above is making trouble to Opera. How
to replace document.all to GetElementById?

See below and [1] and Google. (I suggest some FAQ browsing, for this
very basic stuff.)
What's wrong in innerHTML?

It's not part of the W3C DOM, though understood by most contemporary
browsers with various issues ([2] or Google might be a good starting
point). In your example it's overkill.

Markup: <img id="foo" ...>

JS: document.getElementById("foo").src = "newpic.jpg";

[1]
http://developer.mozilla.org/en/docs/DOM:document.getElementById
[2]
http://developer.mozilla.org/en/docs/DOM:element.innerHTML

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Fox meinte:
What's wrong in innerHTML?

It's not part of the W3C DOM, though understood by most contemporary
browsers with various issues ([2] or Google might be a good starting
point). In your example it's overkill.

Markup: <img id="foo" ...>

JS: document.getElementById("foo").src = "newpic.jpg";

It is more compatible (back to DOM Level 0) to give the `img' element
a name (here: name="foo") and then use

document.images["foo"].src = "newpic.jpg";

Although with current UAs the `images' collection also works with IDs,
as specified in W3C DOM Level 2 HTML.


PointedEars
 
F

Fox

Okay, the slideshow script is working now on IE / FF and Opera :)
Thank you, Gregor, PointedEars, for your valuable help!

Here is the final version of the slideshow script, for any JS beginner
(like me) who would need it ;-)

<script type='text/javascript'>
i=2
function NextImg(){
if (i>34) i=1;
document.images["screenshot"].src = "../snapshot/100in1/snap"+i
+".png";
i++;
}
setInterval("NextImg()",3000)
</script>
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top