javascript with Firefox & IE

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
FireLord
Forum Newbie
Posts: 6
Joined: Fri Mar 20, 2009 5:44 am

javascript with Firefox & IE

Post 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:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: javascript with Firefox & IE

Post by Chris Corbyn »

:arrow: Moved to Client Side
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: javascript with Firefox & IE

Post by Chris Corbyn »

How are you calling this function? What type of variable is "table" ?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: javascript with Firefox & IE

Post 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).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: javascript with Firefox & IE

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: javascript with Firefox & IE

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: javascript with Firefox & IE

Post 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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: javascript with Firefox & IE

Post 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.
Post Reply