Script Works in IE not FF--What am I missing?

D

DavidB

Hi all

I have a script that works perfectly in IE but not in FF. I am sure
that the problem is easy to resolve, but I seem to be too dumb to
figure it out.

<html>

<head>
<script language="javascript">
var msgIX = 0;
var msgs = [
" Notices and Messages",
"Mad Scientist Camps are July 22 and July 29",
" Notices and Messages",
"Summer Beach Blast is August 19 ",
" Notices and Messages",
"Family Movie Night is August 25 ",
" Notices and Messages",
"Pub Night and Quiz is October 14 or 21 (TBD) ",
" Notices and Messages",
"Pot Luck Dinner is November--Exact Date TBD ",
" Notices and Messages",
"-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"];
function scrollMessages(milliseconds) {
window.setInterval ("displayMessage()", milliseconds);
}
function displayMessage() {
if (document.getElementByID != null) {
var heading = document.getElementById("scrollme");
heading.firstChild.nodeValue = msgs[msgIX];
}else{
if(navigator.appName == "Microsoft Internet Explorer") {
var heading = document.all.item("scrollme");
heading.innerText = msgs[msgIX];
}
}
++msgIX;
msgIX %= msgs.length;
}
</script>

</head>

<body onload="scrollMessages(2000)">

<p id="scrollme">...</p>
</body>

</html>

Any assistance would be greatly appreciated. I was somehow volunteered to
create a community website, and this is one of the requested features.

DB
 
W

web.dev

DavidB said:
Hi all

I have a script that works perfectly in IE but not in FF. I am sure
that the problem is easy to resolve, but I seem to be too dumb to
figure it out.

"This doesn't work" is a useless message. Next time please include
details on what exactly doesn't work, what you expect to behave and
perhaps how it actually behaves.
<script language="javascript">

The language attribute is deprecated in favor of the the type
attribute:

function displayMessage() {
if (document.getElementByID != null) {

This line is the culprit. It is document.getElementById. Please note
the lowercase 'd'.
if(navigator.appName == "Microsoft Internet Explorer") {

This is error-prone since browsers these days can spoof what they are.
Use object detection instead.
Any assistance would be greatly appreciated. I was somehow volunteered to
create a community website, and this is one of the requested features.

DB

This is the reason why it "doesn't work".

1. In your logic, the first if statement will always return false
because of the reason above, thus your code statements will never be
executed.
2. In your else block, the code statements will work on IE because, as
your logic indicates, the browser is IE.
3. FF doesn't work because of reason #1 + #2.

Both browsers support document.getElementById, so you can merely test
for this functionality.

if(document.getElementById)
{
//TODO: update message
}
 
C

cwdjrxyz

DavidB said:
Hi all

I have a script that works perfectly in IE but not in FF. I am sure
that the problem is easy to resolve, but I seem to be too dumb to
figure it out.

<html>

<head>
<script language="javascript">
var msgIX = 0;
var msgs = [
" Notices and Messages",
"Mad Scientist Camps are July 22 and July 29",
" Notices and Messages",
"Summer Beach Blast is August 19 ",
" Notices and Messages",
"Family Movie Night is August 25 ",
" Notices and Messages",
"Pub Night and Quiz is October 14 or 21 (TBD) ",
" Notices and Messages",
"Pot Luck Dinner is November--Exact Date TBD ",
" Notices and Messages",
"-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"];
function scrollMessages(milliseconds) {
window.setInterval ("displayMessage()", milliseconds);
}
function displayMessage() {
if (document.getElementByID != null) {
var heading = document.getElementById("scrollme");
heading.firstChild.nodeValue = msgs[msgIX];
}else{
if(navigator.appName == "Microsoft Internet Explorer") {
var heading = document.all.item("scrollme");
heading.innerText = msgs[msgIX];
}
}
++msgIX;
msgIX %= msgs.length;
}
</script>

</head>

<body onload="scrollMessages(2000)">

<p id="scrollme">...</p>
</body>

</html>

I don't have time at the moment to study your script in detail. However
I do see a possible problem or two. First the script does not work on
Opera either. Second, you use if(navigator.appName == "Microsoft
Internet Explorer") for detection on one leg of the else statement.
This is no longer reliable. Many browsers now come set or can be set to
spoof Microsoft Internet Explorer to avoid lockouts by such detection
done in many cases when not necessary. I believe that most recent
Mozilla family (Firefox, Netscape, Mozilla) and Opera browsers can be
and often are set to spoof Microsoft. Thirdly, and this would not cause
your problem, is we now use <script type="text/javascript"> and do not
use language.
 
L

Lasse Reichstein Nielsen

DavidB said:
I have a script that works perfectly in IE but not in FF. ....
if (document.getElementByID != null) {

this is always false ... but it you wrote "Id" with a lower case "d",
it would work in all current browsers.
var heading = document.getElementById("scrollme");
heading.firstChild.nodeValue = msgs[msgIX];
}else{
if(navigator.appName == "Microsoft Internet Explorer") {

This else branch would then only apply to IE4.

Any assistance would be greatly appreciated. I was somehow volunteered to
create a community website, and this is one of the requested features.

Things that move by themselves is a bad idea. It makes it harder to read
for no good reason, and it makes an unnecessary dependency on javscript
for presenting text. Just put the text there.

/L
 
D

DavidB

I don't have time at the moment to study your script in detail.
However I do see a possible problem or two. First the script does not
work on Opera either. Second, you use if(navigator.appName ==
"Microsoft Internet Explorer") for detection on one leg of the else
statement. This is no longer reliable. Many browsers now come set or
can be set to spoof Microsoft Internet Explorer to avoid lockouts by
such detection done in many cases when not necessary. I believe that
most recent Mozilla family (Firefox, Netscape, Mozilla) and Opera
browsers can be and often are set to spoof Microsoft. Thirdly, and
this would not cause your problem, is we now use <script
type="text/javascript"> and do not use language.

Thanks to all for the help. I am not sure how to fix the issue of spoofing
IE (and I can learn that in time). The problem was fixed in the
document.getElementId typo. One correction and the text displays in both
browsers.

DB
 
C

cwdjrxyz

cwdjrxyz said:
I don't have time at the moment to study your script in detail. However
I do see a possible problem or two. First the script does not work on
Opera either. Second, you use if(navigator.appName == "Microsoft
Internet Explorer") for detection on one leg of the else statement.
This is no longer reliable. Many browsers now come set or can be set to
spoof Microsoft Internet Explorer to avoid lockouts by such detection
done in many cases when not necessary. I believe that most recent
Mozilla family (Firefox, Netscape, Mozilla) and Opera browsers can be
and often are set to spoof Microsoft. Thirdly, and this would not cause
your problem, is we now use <script type="text/javascript"> and do not
use language.

Look at http://www.cwdjr.net/test/scrollMod1.html and view the source.
The getElementById branch works perfectly well for IE6 as well as the
modern Mozilla family browsers and Opera. Thus you do not need the
document.all branch unless you want to include very old IE browsers
such as IE4. If that is a problem, then it might be possible to use
Microsoft conditional statements to write one script if the browser is
IE and another if anything else. This would be far more reliable than
attempting to detect IE with script. So far as I know, the Microsoft
conditional statement approach has not been spoofed, but it likely
could be.
 
R

RobG

DavidB wrote:
[...]
Thanks to all for the help. I am not sure how to fix the issue of spoofing
IE (and I can learn that in time).

Simple: ditch browser detection altogether:

if (document.getElementById){
/* use document.getElementById */
} else if (document.all){
/* use document.all */
}

You may want to add support for old Navigator by supporting layers, but
maybe not. Read the FAQ:

<URL:http://www.jibbering.com/faq>
 
C

cwdjrxyz

RobG said:
DavidB wrote:
[...]
Thanks to all for the help. I am not sure how to fix the issue of spoofing
IE (and I can learn that in time).

Simple: ditch browser detection altogether:

if (document.getElementById){
/* use document.getElementById */
} else if (document.all){
/* use document.all */
}

Of course IE6 supports both document.getElementById and document.all,
and IE4 supports only document.all. If the document.getElementById leg
also works properly on IE5, which it should but which I can not test
for lack of IE5, then a more elegant way might be to route to the
document.getElementById path if both document.getElementById and
document.all are supported and route to a document.all path if only it
is supported.

Of course, if one wanted to support ancient relic browsers, one might
use an alternate path to do something else. I fail to see the need to
go to the trouble of supporting IE4, Netscape4, and earlier browsers,
but there could still be a few small areas of the world where such
support might still be of use. I know that where I live that such old
relic browsers would not allow me into my bank site, important
government sites, many shopping sites, or my broker. Also they will not
work on many important news and media sites.
 
R

RobG

cwdjrxyz said:
RobG said:
DavidB wrote:
[...]
Thanks to all for the help. I am not sure how to fix the issue of spoofing
IE (and I can learn that in time).
Simple: ditch browser detection altogether:

if (document.getElementById){
/* use document.getElementById */
} else if (document.all){
/* use document.all */
}

Of course IE6 supports both document.getElementById and document.all,
and IE4 supports only document.all. If the document.getElementById leg
also works properly on IE5, which it should but which I can not test
for lack of IE5, then a more elegant way might be to route to the
document.getElementById path if both document.getElementById and
document.all are supported and route to a document.all path if only it
is supported.

That seems confused - what about browsers that support getElementById
but not document.all?

The *only* reason to test for document.all is as a fall-back if
document.getElementById is not supported. If the latter is supported
(which it is in perhaps 99.9% of browsers in use), there is no need to
test for the former.


[...]
 
C

cwdjrxyz

RobG said:
cwdjrxyz said:
RobG said:
DavidB wrote:
[...]
Thanks to all for the help. I am not sure how to fix the issue of spoofing
IE (and I can learn that in time).
Simple: ditch browser detection altogether:

if (document.getElementById){
/* use document.getElementById */
} else if (document.all){
/* use document.all */
}

Of course IE6 supports both document.getElementById and document.all,
and IE4 supports only document.all. If the document.getElementById leg
also works properly on IE5, which it should but which I can not test
for lack of IE5, then a more elegant way might be to route to the
document.getElementById path if both document.getElementById and
document.all are supported and route to a document.all path if only it
is supported.

That seems confused - what about browsers that support getElementById
but not document.all?

The *only* reason to test for document.all is as a fall-back if
document.getElementById is not supported. If the latter is supported
(which it is in perhaps 99.9% of browsers in use), there is no need to
test for the former.

I see what you are talking about. I failed to notice that your test for
document.all was an "else if" rather than just "else", which makes a
considerable difference. Still I can see a few cases where one might
want a more extensive test. Besides IE, even recent Opera browsers will
support document.all in addition to getElementById. I doubt if there
ever was an early Opera browser that only supported document.all, but
since I had no experience with them, I could not say for sure. Also
earlier versions of WebTV supported only document.all - or at least
some parts of it. The point is that it can not be assumed that one is
dealing with an IE browser if it supports document.all with or without
document.getElementById support. At least in the case of WebTV one
could test for appCodeName. For most computer browsers, this returns
Mozilla, which is not at all useful. However WebTV returned "bowser",
which allowed you provide a special path for WebTV to overcome the many
things it did not support.

In a few cases, the Microsoft html conditional comment also can be
useful. Such a comment appears as just an ordinary comment to most
browsers, but Microsoft browsers can see through the comment tags to
view code within. This becomes more interesting when one notices that
Microsoft conditional comments can be used to write one script when the
browser is Microsoft and another when it is not. This approach can be
useful when one wishes to use some of the many IE specific codes only
if one is viewed by an IE browser and use something else for other
browsers. I have used such an approach to allow Microsoft to use an
ActiveX WMP object for playing certain media files, and for using an
ordinary object for other browsers which often do not support ActiveX.
I found one case when a video file would start streaming at once on
most browsers using an ordinary object, but it would not stream until
completly downloaded on IE6. Allowing the ActiveX object for IE only
corrected this problem, and streaming started at once. So far as I
know, the Microsoft conditional comment has not been spoofed, but it
likely could be if other browser writers wanted to.
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top