473,412 Members | 5,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Code not working with FireFox

Hi,

Here's a scaled down version of what I'm working with. It execututes
correctly in IE, but FireFox fails to run it. The JavaScript Console
reports the error "Error: myform.getElementsByTagName("TD")[1] has no
properties"
Why isn't this working? If it's a syntax issue, is there an alternate
method that works across both browsers?

<html>
<head>
<title>My Document</title>
<script type="text/javascript">
function change()
{
newprice=123.45;

var myform = document.getElementsByTagName("FORM")[0];
myform.getElementsByTagName("TD")[1].innerHTML = '<b>'+newprice+'
EUR </b>';

myform=document.forms[(document.getElementsByTagName
("FORM").length-1)];
myform.getElementsByTagName("TD")[1].innerHTML='<b>'+newprice+' EUR
</b>';

}
</script>

</head>
<body>

<table ><tr><td ></td></tr>
<form >
<tr><td ></td><td ><b>0,34 EUR</b></td></tr></form>
</table>

<!-- MORE HTML CODE HERE --->

<table ><form >
<tr><td ></td><td ><b>0,34 EUR</b></td></tr>
<tr><td><input type="button" name="Submit" value="change"
onclick="change()">
Change form data</td></tr></form></table>

</body>
</head>
Apr 15 '06 #1
4 2980
Martínez wrote:
Hi,

Here's a scaled down version of what I'm working with. It
execututes correctly in IE, but FireFox fails to run it.
The JavaScript Console reports the error
"Error: myform.getElementsByTagName("TD")[1] has no properties"
Why isn't this working? If it's a syntax issue, is there an
alternate method that works across both browsers?
You can do considerably better than just two browser here.

<snip> <body>

<table ><tr><td ></td></tr>
<form >
<tr><td ></td><td ><b>0,34 EUR</b></td></tr></form>
</table>

<!-- MORE HTML CODE HERE --->

<table ><form >
<tr><td ></td><td ><b>0,34 EUR</b></td></tr>
<tr><td><input type="button" name="Submit" value="change"
onclick="change()">
Change form data</td></tr></form></table>

<snip>

I am afraid you problem is the direct consequence of using invalid
mark-up. The relationship between mark-up validity and scripting is that
you will be scripting a DOM and when you need to script it in a way that
is interested in the structure of the DOM (as you are here) it is quite
important that the DOMs you interact with on different browsers are
structurally the same.

Formal validity is not strictly necessary in order to get browsers to
produce structurally consistent DOMs. However, structural validity is
critical. HTML specifies that particular elements may only appear in
particular contexts, your problem here is that a FORM element may not be
a child of a TABLE. With structurally valid HTML there are well
specified and understood rules for translating an HTML document into a
DOM structure, and because those rules are well specified their
application in browsers does result in consistent DOM structures.

When an HTML document is not structurally valid the browser cannot
follow the specified rules and instead it follows error-correction
rules, in an effort to do the best possible job with the document it is
given. Error-correction rules could never be formally specified, indeed
they will often be proprietary secrets known only to the browser's
authors. As a result it is actually unlikely that any two browsers will
do their error-correction in the same way, and they don't.

In the case of Mozilla, encountering your FORM elements in a context
where they are not aloud, the browser moves the form element to the
first preceding location in the DOM in which it would be legal. However,
the TABLE and its contents are not moved so when you attempt to find TDs
that are descendants of FORM you will not find any as the FORM is now
outside the TABLE.

Simply, you fix the problem by moving the opening and closing FORM tags
so they surround the pertinent table. In the long run you will benefit
more from learning the formal rules that apply to HTML structure and
write only structurally valid HTML document. Doing so will significantly
reduce the effort you would have to put into scripting those documents.

Richard.
Apr 15 '06 #2
"Martínez" <ma******@martinez.com> writes:
The JavaScript Console
reports the error "Error: myform.getElementsByTagName("TD")[1] has no
properties" Why isn't this working? If it's a syntax issue, is there an alternate
method that works across both browsers?

It's a syntax issue. Your HTML is not valid, and the browsers try
to make sense out of it and ends up doing so in different ways.
var myform = document.getElementsByTagName("FORM")[0];
"document.forms[0]" is just as good and standards compliant.
.... myform=document.forms[(document.getElementsByTagName
("FORM").length-1)];
And that's mixing idioms. Just do:
myform = document.forms[document.forms.length-1];

<table ><tr><td ></td></tr>
<form >
<tr><td ></td><td ><b>0,34 EUR</b></td></tr></form>


The only allowed children of a table element are table components:
tbody, thead, tfoot, caption, col, colgroup, and tr
(and actually "tr" can't be a direct child of a table element, it's
just that both start and end tag of "tbody" can be omitted).

So you can't put a "form" element there. Nor can a "tr" element be a
child of a form. Firefox tries to correct the error, and it appears
that the rule saying that a "tr" can only be inside a table wins.
What Firefox treats the table as, is:
---
<table><tbody><tr><td></td></tr>
<form></form>
<tr><td></td><td><b>0,34 EUR</b></td></tr>
</tbody></table>
---
As you can see, the form element is ended before the unexpected "tr".
A solution would be to wrap the form around the entire table:
<form action="">
<table>
<tr><td></td></tr>
<tr><td></td><td><b>0,34 EUR</b></td></tr>
</table>
</form>

Also, "form" is probably not the correct element here, since there are
no form controls inside it. Put an id on the table and work directly
on that instead.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 16 '06 #3
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in
news:e1*******************@news.demon.co.uk:

Simply, you fix the problem by moving the opening and closing FORM
tags so they surround the pertinent table. In the long run you will
benefit more from learning the formal rules that apply to HTML
structure and write only structurally valid HTML document. Doing so
will significantly reduce the effort you would have to put into
scripting those documents.

Richard.

Thanks Richard, its working after seeing your follow up post.
Apr 16 '06 #4
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in
news:sl**********@hotpop.com:



A solution would be to wrap the form around the entire table:
<form action="">
<table>
<tr><td></td></tr>
<tr><td></td><td><b>0,34 EUR</b></td></tr>
</table>
</form>


I have restructured the HTML code after your suggestion and it works
fine now, thanks
Apr 16 '06 #5

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

Similar topics

8
by: Gregory Piñero | last post by:
Hi guys, I'm trying to run this statement: os.system(r'"C:\Program Files\Mozilla Firefox\firefox.exe"' + ' "www.blendedtechnologies.com"') The goal is to have firefox open to that website....
3
by: The Bicycling Guitarist | last post by:
http://www.TheBicyclingGuitarist.net/css/index.css I have red nav buttons that turn blue when hovered over and turn purple after being visited. The red and blue work in FireFox, but not the visited...
6
by: Mark Olbert | last post by:
The doPostBack javascript functioning is not submitting the page when called by linkbuttons (or an autopostback checkbox, for that matter). I'm aware of a problem with Netscape browsers and the...
7
by: Coder | last post by:
Hi I have the following code in java script, it is not giving proper output in FIREFOX but running fine in IE... can anybody help me out to make this run in FIREFOX . <script...
1
by: ajaysoniji | last post by:
Hi, I am using this code to read the option values from a select option. I am getting the reference of that select option in arg1. Its working fine with mozilla firefox but not with Internet...
9
by: javakid | last post by:
Hi Following form validatioin code is working fine on IE but not working on Mozilla Firefox V2. Can any body suggest? Regards <script ="JAVASCRIPT" type="text/javascript">...
5
by: CoolForU | last post by:
this is my code and i call this code from code behinde file on page load like this useragree.Attributes.Add("onclick", "CheckSelection();") this is working fine with IE but not working with...
3
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
1
by: Sura | last post by:
Hi I have a flash interactive window which has html links and this appears on an html page. This window can be moved with the mouse on the html page. The html page has an iFrame too. When the...
26
by: Jenitha S | last post by:
Hi, I am working in JSP code.A Menubar is displayed in JSP page.It's working fine in IE. But in case of firefox the very first menuitem is not working.If i move that menu item as second means it's...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...
0
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...

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.