Page 1 of 1

javascript with Firefox & IE

Posted: Mon Mar 30, 2009 8:26 am
by FireLord
Hi all, i've a problem with javascript.

The code is:

Code: Select all

function do_add_filter(table){
 
    var arr_labels = new Array();
    var arr_values = new Array();
 
    // ------
    // changhe this with che correct objects
    objForm = document.form1;
    tot = parseInt(document.form1.hid_count.value, 10);
    // ------
 
    for(i=0; i<tot; i++){
        if ( objForm.elements["chk_" + i].checked == true   ){
            arr_labels[arr_labels.length] = objForm.elements["hid_" + i].value;
            arr_values[arr_values.length] = objForm.elements["chk_" + i].value;
        }
    }
 
    if (window.opener == undefined) return false;
    if (window.opener.document == undefined) return false;
 
    var parentdoc = window.opener.document;
 
    var parent_field = parentdoc.getElementById("hid_"+table);
    if (parent_field == undefined) return false;
    parent_field.value = arr_values.join(',');
 
    var parent_labels = parentdoc.getElementById("label_"+table);
    if (parent_labels == undefined) return false;
    parent_labels.innerHTML = arr_labels.join('; ') + "&nbsp;";
 
    var parent_style = parent_labels.style;
    parent_style.whiteSpace = "normal";
 
    window.close();
}
IE does it correctly but Mozilla makes me problems ('cause when I the press the button, that calls do_add_filter function, nothing happen).
I did some test and i found that the problem is in the string:

Code: Select all

    var parent_field = parentdoc.getElementById("hid_"+table);
With IE parent_field contains "[object]".
and in Firefox it contains "null".

How can I solve it? :cry:

Re: javascript with Firefox & IE

Posted: Mon Mar 30, 2009 9:14 am
by Chris Corbyn
:arrow: Moved to Client Side

Re: javascript with Firefox & IE

Posted: Mon Mar 30, 2009 9:17 am
by Chris Corbyn
How are you calling this function? What type of variable is "table" ?

Re: javascript with Firefox & IE

Posted: Mon Mar 30, 2009 8:30 pm
by JAB Creations
You're making the same mistake every web designer makes when they first start out: you think IE is doing it correctly but every other browser isn't. IE is built to support crappy code really well but standards compliant code is another story. Take it from a more experienced web designer, you'll appreciate Firefox a whole lot more once you start to realize it's doing exactly what you're telling it.

Chris is correct, what the heck is "table"? In general I've found it problematic to do...

Code: Select all

var num = '3';document.getElementById('example_'+num);
I stick to doing it like...

Code: Select all

var num = '3';var example = 'example_'+num;document.getElementById(example);
Use object detection and alerts to your advantage.

Code: Select all

if (document.getElementById(example)) {alert('There is an element with the id of \'example\'.');}else if (!document.getElementById(example)) {alert('There is ***BO*** element with the id of \'example\'.');}
...you don't need the extra if statement there however it's to show you how to detect if something does not exist (as compared to regular object detection).

Re: javascript with Firefox & IE

Posted: Tue Mar 31, 2009 1:27 am
by Chris Corbyn
JAB Creations wrote:

Code: Select all

var num = '3';document.getElementById('example_'+num);
I stick to doing it like...

Code: Select all

var num = '3';var example = 'example_'+num;document.getElementById(example);
I don't buy that sorry. They both do the same thing and it's almost always better to eliminate temporary variables from code in the interest of being able to refactor more easily.

Similarly, there are better ways to debug than to use alert() everywhere... which can be extremely frustrating if you've got a perpetual loop or recursion somewhere. Use console.log() in Firefox/FireBug instead.

Re: javascript with Firefox & IE

Posted: Tue Mar 31, 2009 1:43 am
by Benjamin
FireLord wrote:With IE parent_field contains "[object]".
and in Firefox it contains "null".

How can I solve it? :cry:

What is the value of table in both browsers? Have you checked?

Re: javascript with Firefox & IE

Posted: Thu Apr 09, 2009 1:24 am
by JAB Creations
Chris Corbyn wrote:I don't buy that sorry.

Chris, in a script I've been working on tonight exclusively to fix IE6 this does not work...

Code: Select all

document.getElementById(b).childNodes(document.getElementById(b).childNodes.length).lastChild.attachEvent('onblur', function() {change(a,'menu');});
...while this does...

Code: Select all

var a6 = document.getElementById(b).childNodes.length;var b6=3;document.getElementById(b).childNodes(b6).lastChild.attachEvent('onblur', function() {change(a,'menu');});
IE is very difficult when it comes to doing things dynamically. Additionally I had to pass the variable twice before it would just work.

Re: javascript with Firefox & IE

Posted: Thu Apr 09, 2009 12:48 pm
by kaszu
@jab It probably doesn't work, because document.getElementById(b) last child is not html node. In IE child nodes of an element may not be the same as in other browsers (from experience). childNodes is a list of all nodes, not only html nodes.