element.children has no properties (mozilla only)

L

Luke Dalessandro

I'm not sure if this is the correct forum for platform specific
(Mozilla/Firefox) javascript problems, so just shout and point me to the
correct newsgroup if I'm being bad.

Here's the deal...

html file (generated using .NET 2.0 beta2):

<form method="post" action="Test2.aspx" id="form1">

<div>
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

<select name="ddl" id="ddl">
<option value="hi">hi</option>
<option value="there">there</option>
</select>

...BLAH BLAH BLAH

...a bunch of anchors with "javascript:" hrefs.

<script type="text/javascript">
<!--
var pageUrl='/Test2.aspx'; // localhost
WebForm_InitCallback(); // <-------- Error in here
...more js
-->
</script>
</form>

Javascript causing error:

function WebForm_InitCallback() {
var count = theForm.elements.length;
var element;
for (var i = 0; i < count; i++) {
element = theForm.elements;
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if (type == "text" || type == "hidden" || type ==
"password" ||
((type == "checkbox" || type == "radio") &&
element.checked)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
else if (tagName == "select") {

// ***ERROR***
var selectCount = element.children.length; // <--
// ***ERROR***

for (var j = 0; j < selectCount; j++) {
var selectChild = element.children[j];
if ((selectChild.tagName.toLowerCase() == "option")
&& (selectChild.selected == true)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(selectChild.value) + "&";
}
}
}
else if (tagName == "textarea") {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
}

This clears IE fine, but FireFox 1.0.4 tosses the error

Error: element.children has no properties
Source File: ...

Does anyone know if there is something special about FireFox's "select"
element.children property that would cause this?

As a background, this code is autogenerated by ASP.NET in conjunction
with the TreeView control when it is set to use out-of-band callbacks to
populate TreeNodes... I can provide a live demo if you contact me directly.

Thanks in advance,
Luke Dalessandro
 
M

Martin Honnen

Luke Dalessandro wrote:

else if (tagName == "select") {

// ***ERROR***
var selectCount = element.children.length; // <--
// ***ERROR***

children is part of the IE DOM since IE 4 but it is not part of the W3C
DOM that Mozilla implements. The closest in that object model is
childNodes which contains all child nodes (including text nodes) while
children contains all child elements.

As for that code I would simply loop through element.options once it has
been established that element is a select element.
 
L

Luke Dalessandro

Martin,

Thanks for the pointer... unfortunately, I have no control over the
code. The script is autogenerated through a call to a predetermined
handler in .NET (ie, the framework automatically add a

<script
src="/WebResource.axd?d=MSFJ4SIWhD3cinjlZxIy4w2&amp;t=632546115913125000"
type="text/javascript"></script>

tag when rendering the page.

Looks like MS is continuing to write broken code. sigh...

Luke
 
D

Diego

Hi Luke,

I use this piece of code to correct the problem:
<code>
if (typeof Node != 'undefined') {
if (typeof Node.children == 'undefined') {
eval('Node.prototype.children getter = function() {return
this.childNodes;}');
}
}
</code>
And wait until MS fixes the problem.

Diego

Luke Dalessandro said:
I'm not sure if this is the correct forum for platform specific
(Mozilla/Firefox) javascript problems, so just shout and point me to the
correct newsgroup if I'm being bad.

Here's the deal...

html file (generated using .NET 2.0 beta2):

<form method="post" action="Test2.aspx" id="form1">

<div>
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

<select name="ddl" id="ddl">
<option value="hi">hi</option>
<option value="there">there</option>
</select>

...BLAH BLAH BLAH

...a bunch of anchors with "javascript:" hrefs.

<script type="text/javascript">
<!--
var pageUrl='/Test2.aspx'; // localhost
WebForm_InitCallback(); // <-------- Error in here
...more js
-->
</script>
</form>

Javascript causing error:

function WebForm_InitCallback() {
var count = theForm.elements.length;
var element;
for (var i = 0; i < count; i++) {
element = theForm.elements;
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if (type == "text" || type == "hidden" || type ==
"password" ||
((type == "checkbox" || type == "radio") &&
element.checked)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
else if (tagName == "select") {

// ***ERROR***
var selectCount = element.children.length; // <--
// ***ERROR***

for (var j = 0; j < selectCount; j++) {
var selectChild = element.children[j];
if ((selectChild.tagName.toLowerCase() == "option")
&& (selectChild.selected == true)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(selectChild.value) + "&";
}
}
}
else if (tagName == "textarea") {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
}

This clears IE fine, but FireFox 1.0.4 tosses the error

Error: element.children has no properties
Source File: ...

Does anyone know if there is something special about FireFox's "select"
element.children property that would cause this?

As a background, this code is autogenerated by ASP.NET in conjunction
with the TreeView control when it is set to use out-of-band callbacks to
populate TreeNodes... I can provide a live demo if you contact me directly.

Thanks in advance,
Luke Dalessandro
 
Z

zoneboy

I had the same problem myself. You can work around the problem by
adding a call to a function like the one I've written below. I
basically just added a check to see if the children property exist.

If you have a <asp:button> on your page just do this in the codebehind:

mybutton.Attributes.Add( "onclick", "PrepareForPostBack();" );

This should ensure it would run the Prepare function before doing the
actual postback.

Regards,
Mikael Svenson
C# Developer


-JAVASCRIPT--

function PrepareForPostBack()
{
try { WebForm_InitCallback();}
catch( e )
{
WebForm_InitCallback_Mozilla();
}
}

function WebForm_InitCallback_Mozilla() {
var count = theForm.elements.length;
var element;
for (var i = 0; i < count; i++) {
element = theForm.elements;
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if (type == "text" || type == "hidden" || type ==
"password" ||
((type == "checkbox" || type == "radio") &&
element.checked)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
else if (tagName == "select") {
var children;
if( typeof element.children == 'undefined' ) {
<!-- We are using mozilla -->
children = element.childNodes;
}
else
{
<!-- we are using IE -->
children = element.children;
}

var selectCount = children.length;
for (var j = 0; j < selectCount; j++) {
var selectChild = children[j];
if ((selectChild.tagName.toLowerCase() == "option") &&
(selectChild.selected == true)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(selectChild.value) + "&";
}
}
}
else if (tagName == "textarea") {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
}
Hi Luke,

I use this piece of code to correct the problem:
<code>
if (typeof Node != 'undefined') {
if (typeof Node.children == 'undefined') {
eval('Node.prototype.children getter = function() {return
this.childNodes;}');
}
}
</code>
And wait until MS fixes the problem.

Diego

Luke Dalessandro said:
I'm not sure if this is the correct forum for platform specific
(Mozilla/Firefox) javascript problems, so just shout and point me to the
correct newsgroup if I'm being bad.

Here's the deal...

html file (generated using .NET 2.0 beta2):

<form method="post" action="Test2.aspx" id="form1">

<div>
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

<select name="ddl" id="ddl">
<option value="hi">hi</option>
<option value="there">there</option>
</select>

...BLAH BLAH BLAH

...a bunch of anchors with "javascript:" hrefs.

<script type="text/javascript">
<!--
var pageUrl='/Test2.aspx'; // localhost
WebForm_InitCallback(); // <-------- Error in here
...more js
-->
</script>
</form>

Javascript causing error:

function WebForm_InitCallback() {
var count = theForm.elements.length;
var element;
for (var i = 0; i < count; i++) {
element = theForm.elements;
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if (type == "text" || type == "hidden" || type ==
"password" ||
((type == "checkbox" || type == "radio") &&
element.checked)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
else if (tagName == "select") {

// ***ERROR***
var selectCount = element.children.length; // <--
// ***ERROR***

for (var j = 0; j < selectCount; j++) {
var selectChild = element.children[j];
if ((selectChild.tagName.toLowerCase() == "option")
&& (selectChild.selected == true)) {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(selectChild.value) + "&";
}
}
}
else if (tagName == "textarea") {
__theFormPostData += element.name + "=" +
WebForm_EncodeCallback(element.value) + "&";
}
}
}

This clears IE fine, but FireFox 1.0.4 tosses the error

Error: element.children has no properties
Source File: ...

Does anyone know if there is something special about FireFox's "select"
element.children property that would cause this?

As a background, this code is autogenerated by ASP.NET in conjunction
with the TreeView control when it is set to use out-of-band callbacks to
populate TreeNodes... I can provide a live demo if you contact me directly.

Thanks in advance,
Luke Dalessandro
 
M

Mikael Svenson

Forgot to mention that you have to do

_theFormPostData = '';

before running WebForm_InitCallback, or put it as the first line. If
not you will get double values.

Regards,
Mikael Svenson
C# developer
 
A

ASM

Luke said:
I'm not sure if this is the correct forum for platform specific
(Mozilla/Firefox) javascript problems, so just shout and point me to the
correct newsgroup if I'm being bad.

Here's the deal...

html file (generated using .NET 2.0 beta2):

<form method="post" action="Test2.aspx" id="form1">

<div>
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
<input type="hidden" ... />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;

theForm.target = enventTarget
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

<select name="ddl" id="ddl">
<option value="hi">hi</option>
<option value="there">there</option>
</select>

...BLAH BLAH BLAH

...a bunch of anchors with "javascript:" hrefs.

<script type="text/javascript">
<!--
var pageUrl='/Test2.aspx'; // localhost
WebForm_InitCallback(); // <-------- Error in here
...more js
-->
</script>
</form>

Javascript causing error:

function WebForm_InitCallback() {

and with some less lines and calabistic words ?

var __theFormPostData = ''; // if absolutly needed

function WebForm_InitCallback() {
// would prefer to set theForm here
// theForm = document.forms[0];
var F = theForm; // hope theForm was ok ! ?
var D = '';
for(var i=0;i<F.length;i++) {
if(F.tagName.toLowerCase()=='select') {
if(F.selectedIndex!=0)
D += F.name+'='+F.options[F.selectedIndex].value+'&';
}
if(F.tagName.toLowerCase()=='textarea')
if(F.value!='')
D += F.name+'='+escape(F.value)+'&';
var e = F.type.toLowerCase();
if((e=='text'||e=='password')&&e.value!=''||
(e=='checkbox'||e=='radio')&&F.checked)
D += F.name+'='+escape(F.value)+'&';
}
if(D=='') alert('Form empty');
else D = D.substring(0,D.length-1)
__theFormPostData = D;
}
As a background, this code is autogenerated by ASP.NET in conjunction
with the TreeView control when it is set to use out-of-band callbacks to
populate TreeNodes... I can provide a live demo if you contact me directly.

if it works like that : No, thank you very very much
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top