473,898 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript Document Code Not Cross Platform


I have code that works in IE 6 but does not work in FireFox 1.0.2, how
can I change it so it works in both browsers?
<html>
<head>
<script language="JavaS cript" type="text/JavaScript">
function AttributeSelect ed()
{
var selectVal = document.forms['testspit'].elements['lsbRA'].options;
if (selectVal.valu e == 'Item1')
{
document.getEle mentById('text2 ').style.visibi lity='visible';
document.getEle mentById('spnla bel').style.vis ibility='visibl e';
}
else
{
document.getEle mentById('text2 ').style.visibi lity='hidden';
document.getEle mentById('spnla bel').style.vis ibility='hidden ';
}
}
</script>
</head>
<body>
<form name="testspit" method="post" action="">
<div id="divPlace" style="width:90 %;left:10px;top :40px">
<select multiple id="lsbRA" onChange="Attri buteSelected(); " >
<option value="Item1">I tem 1</option>
<option value="Item2">I tem 2 </option>
</select>
<br/><br/>
<input name="text1" type="text" value="text1" size="20">
<br/><br/>
<span id="spnlabel" style="overflow :hidden">
-
</span>
<span id="spntext" style="overflow :hidden">
<input name="text2" type="text" value="text2" size="20">
</span>
</div>
</body>
</html>
Thanks.

Jul 23 '05 #1
8 1836
bradwiseath...@ hotmail.com wrote:
I have code that works in IE 6 but does not work in FireFox 1.0.2, how can I change it so it works in both browsers?


Missing DOCTYPE - that has ramifications here (see below), suggest:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<script language="JavaS cript" type="text/JavaScript">
The language attribute is depreciated, keep the required type
attribute.
function AttributeSelect ed()
{
var selectVal = document.forms['testspit'].elements['lsbRA'].options;

In quirksmode (sans DOCTYPE) IE handles this, Firefox doesn't. If
lsbRA was a single select, then using the above suggested DOCTYPE with:

var selectVal = document.forms['testspit'].elements['lsbRA'];

will fix the issue if 'lsbRA' is a single select - take care, if the
option has no 'value' attribute the value is supposed to be the text,
but IE doesn't understand that.

Adding '.options' on the end returns a collection of the options, which
has no 'value' attribute (it is effectively an array). Removing
'.options' means selectVal will be the element 'lsbRA', whose value
will be that of the selected option.

Since 'lsbRA' is a multiselect, you need to iterate through the options
to find which ones are selected, you can't just get the value. Wrap
your function with a loop that goes through all the options and deals
with the selected ones, so now you have:

var selectVal = document.forms['testspit'].elements['lsbRA'];
for (var i=0, j=selectVal.len gth; i<j; i++){
if (selectVal[i].selected){
alert(selectVal[i].value + ' selected'); // Purely de-bug...
...

if (selectVal.valu e == 'Item1')
becomes:

if (selectVal[i].value == 'Item1')

{
document.getEle mentById('text2 ').style.visibi lity='visible';
Here you are using getElementById on elements that have no ID. IE
treats names and IDs as virtually the same thing, Firefox doesn't
(because they are very different things but overlap somewhat in
purpose). ID and NAME share the same namespace, so if you can't have
different elements with the same name & id. You can, in some
circumstances, have the same name on multiple elements.

The quick 'n dirty solution here is to put the same ID on your elements
as the name you've already given them.
document.getEle mentById('spnla bel').style.vis ibility='visibl e';
}
else
{
document.getEle mentById('text2 ').style.visibi lity='hidden';
document.getEle mentById('spnla bel').style.vis ibility='hidden ';
}
Add a couple of curly braces to close the added for and if loops.

}
}
}
</script>
</head>
<body>
<form name="testspit" method="post" action="">
<div id="divPlace" style="width:90 %;left:10px;top :40px">
<select multiple id="lsbRA" onChange="Attri buteSelected(); " >
<option value="Item1">I tem 1</option>
<option value="Item2">I tem 2 </option>
</select>
<br/><br/>
<input name="text1" type="text" value="text1" size="20">
<input name="text1" id="text1" ... >
<br/><br/>
<span id="spnlabel" style="overflow :hidden">
-
</span>
<span id="spntext" style="overflow :hidden">
<input name="text2" type="text" value="text2" size="20">
<input name="text2" id="text2" ... >
</span>
</div>
</body>
</html>
Thanks.

PS. The logic seems strange, but I'll leave that alone... unless it
isn't doing whatever you expect it to. As it is, it may cause
flickering for some users.
--
Rob

Jul 23 '05 #2
RobG wrote:
bradwiseath...@ hotmail.com wrote: <snip> Missing DOCTYPE - that has ramifications here (see below),
suggest:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<snip>
</select>
<br/><br/>

<snip>

When the mark-up presented appears to XHTML what would suggest an HTML
doctype?

Richard.
Jul 23 '05 #3
That is incredible. Thanks! I just learned a lot. The bottom line is
that I need to show or hide a text box based on a selected option in a
list, and it works in IE6 and FireFox 1.0.2, so I'm set.

Thanks again!

Jul 23 '05 #4
Richard Cornford wrote:
RobG wrote:
bradwiseath.. .@hotmail.com wrote:


<snip>
Missing DOCTYPE - that has ramifications here (see below),
suggest:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">


<snip>
</select>
<br/><br/>


<snip>

When the mark-up presented appears to XHTML what would suggest an HTML
doctype?


I took a punt that the OP was really after HTML.

It's probably reasonable to infer that the OP was attempting XHTML,
but there seems to be an idea spreading that closing empty tags is
desirable in HTML also - it will still validate OK as HTML 4 strict.

There is also no character encoding, title or closing form tag.

--
Rob
Jul 23 '05 #5
On 07/05/2005 02:14, RobG wrote:

[snip]
there seems to be an idea spreading that closing empty tags is
desirable in HTML also - it will still validate OK as HTML 4 strict.
It will validate, yes. However it means something entirely different.
There is also no character encoding [...]


To be fair to the OP, it is preferable to send the character encoding in
the Content-Type HTTP header. You have no way of knowing if the charset
parameter has been specified.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
RobG wrote:
<snip>
I took a punt that the OP was really after HTML.
Given the IE doesn't understand XHTML, HTML would probably be the OP's
best choice regardless of his intention.
It's probably reasonable to infer that the OP was
attempting XHTML, but there seems to be an idea
spreading that closing empty tags is desirable in
HTML also
Many notions that are apparently not harmful are able to freely
propagate. But HTML and XHTML are subject to detailed specification so
they can be understood at a technical level, and they can both be
objectively correct and incorrect. You will be hard pressed to find
anyone with a technical understanding of these mark-up languages
encouraging the mixing of the two styles.
- it will still validate OK as HTML 4 strict.
That depends a great deal on the context of the use of the slash at the
end of the tag. The SGML abbreviation '<br />' expands to '<br>>' ,
where the second chevron is text content following the BR element. The
abbreviated version will validate in contexts where text content would
be allowed, but would not validate if the element-terminating slash
implied the following text chevron in a context that did not allow text
content. The strict DTD has fewer contexts in which text is allowed than
the transition DTD.

Regardless of whether this mixed mark-up validates, it is unlikely that
the OP intended to insert a sequence of '>' characters into their page.
The fact that the majority of browsers do not follow the HTML
specification in this respect conceals the error, but it is still an
error to create mark-up that should be expected to produce results other
than those intended. And even browsers that do not handle the mark-up in
the way that they really should still expend additional effort in
disregarding what they perceive as a superfluous character in a tag.
Thus the folly in mixing mark-up styles should be pointed out when it is
observed, and discouraged.
There is also no character encoding, title or closing
form tag.


As Mike said, character encoding is handled by required HTTP headers so
the absence of an optional (and theoretically superfluous) META element
is not an error. Other obvious mark-up errors could be pointed out to
the OP, or the blanket recommendation to create valid mark-up provided
(though the ill-conceived use of XHTML style mark-up in HTML would need
to be pointed out separately as, as you observer, it will often
validate).

Richard.
Jul 23 '05 #7
Richard Cornford wrote:
RobG wrote:
It's probably reasonable to infer that the OP was
attempting XHTML, but there seems to be an idea
spreading that closing empty tags is desirable in
HTML also


Many notions that are apparently not harmful are able to freely
propagate. But HTML and XHTML are subject to detailed specification so
they can be understood at a technical level, and they can both be
objectively correct and incorrect. You will be hard pressed to find
anyone with a technical understanding of these mark-up languages
encouraging the mixing of the two styles.


<rant>
Yes, I thought that, too, when reading the HTML Compatibility Guidelines
of the XHTML 1.0 Specification. They are flawed for exactly that reason.
</rant>
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
Jul 23 '05 #8
On Sun, 15 May 2005 18:03:00 +0200, Thomas 'PointedEars' Lahn
<Po*********@we b.de> wrote:
<rant>
Yes, I thought that, too, when reading the HTML Compatibility Guidelines
of the XHTML 1.0 Specification. They are flawed for exactly that reason.
</rant>


The W3c is required to address all comments from the public, including
ones that arrive after it becomes a rec, rather than rant pointlessly
on usenet, ww************* @w3.org is the place to send your comments.

Jim.
Jul 23 '05 #9

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

Similar topics

6
2542
by: Andy Fish | last post by:
Hi, I want to use an anchor tag to invoke some javascript and I've read that it's bad form to use <a href="javascript:foo()"> I've read endless usenet posts and hint sites on the net, they all suggest different things and I can't get any kind of consistency, and I can't find any solution that works properly for IE, opera and mozilla. many of the recommended solutions go something like this:
2
1820
by: Halldor Isak Gylfason | last post by:
I am faced with the task of porting a client application to a web architecture platform, e.g. to make it browser based. The technology I used was J2EE, e.g. servlets/JSP pages and HTML/javascript. The application I'm talking about is displaying data from database in grids, roughly speaking from 3 tables, that have parent-child relationship between them, and when you click on a row in a parent grid the content in the child grid should...
2
3273
by: Bob P. | last post by:
Hi, basically what I'd like to do is suppress the action that causes the URL to show up in the browser status bar when I roll over a hyperlink. I know I could use the onmouseover event in each anchor tag, but I'd rather be able to do it globally thru my stylesheet, rather than having to futz with each link. Does anyone know a solution for this? It seems like you could maybe add quick window.status='blah' javascript or something to the...
9
2189
by: MStepansky | last post by:
Whats the difference between Javascript and Java3D? I mean can Javascript do like Java3D can? Or is Java3D on top of Javascript (the core, if thats what it is)? Then I should learn Javascript BEFORE Java3D, right? no? Worse, whats the difference between Java and Javascript? Whats the difference between Visual Basic and Visual Java++? (I still thought Visual Basic is like BASIC programming to allow you to write
7
2288
by: harish.mallipeddi | last post by:
Hi all, This might sound a bit weird but anyways here I go. Recently after witnessing the popularity of AJAX/DHTML, and after enjoying Gmail's fairly cool UI, I'm left wondering...is there going to be a point in the future when Javascript developers would like to hide their sourcecode from prying eyes because they think they'd put in enough efforts into coding and they wouldn't want to give up the commercial value in their code for...
136
9500
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
3
1962
by: sonsofsound77 | last post by:
Hi all I'm new to JavaScript and working my way through the Teach yourself JS 1.5 book. All of the scripts in the book work perfectly apart from the listing 20.3 piano example. http://www.jsworkshop.com/js15/list20-3.html I am using an iMac G5 Mac OSX 10.4.10 Tiger and am getting the error document.embeds.play(); is not a function This is the actual code <title>JavaScript Piano</title>
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9840
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10483
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.