element.children has no properties (mozilla only)

Discussion in 'Javascript' started by Luke Dalessandro, Jun 22, 2005.

  1. 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
     
    Luke Dalessandro, Jun 22, 2005
    #1
    1. Advertising

  2. 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 22, 2005
    #2
    1. Advertising

  3. 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

    Martin Honnen wrote:
    >
    >
    > 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.
    >
     
    Luke Dalessandro, Jun 22, 2005
    #3
  4. Luke Dalessandro

    Diego Guest

    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" <> schreef in bericht
    news:V5fue.1438$...
    > 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
     
    Diego, Jun 23, 2005
    #4
  5. Luke Dalessandro

    Guest

    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) + "&";
    }
    }
    }

    Diego wrote:
    > 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" <> schreef in bericht
    > news:V5fue.1438$...
    > > 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
     
    , Jul 8, 2005
    #5
  6. 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
     
    Mikael Svenson, Jul 8, 2005
    #6
  7. Luke Dalessandro

    ASM Guest

    Luke Dalessandro wrote:
    > 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


    --
    Stephane Moriaux et son [moins] vieux Mac
     
    ASM, Jul 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henri
    Replies:
    9
    Views:
    1,906
    Toby Inkster
    Feb 22, 2005
  2. Matt
    Replies:
    2
    Views:
    889
    Ben Edgington
    Oct 12, 2004
  3. rajesh
    Replies:
    1
    Views:
    99
  4. rajesh
    Replies:
    1
    Views:
    139
    Dr John Stockton
    Jan 13, 2006
  5. okey
    Replies:
    2
    Views:
    175
    David Mark
    May 24, 2009
Loading...

Share This Page