473,396 Members | 2,009 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,396 software developers and data experts.

Javascript Variables and Firefox

I have a script that loops through an existing table list and prepares
each href element node to trigger a function when an image is
clicked. The function that will be run passes a property value to the
function upon clicking. The property value is one of the attributes
defined for each respective href in the property list.

In IE and Netscape, the property values pass correctly, but in
Firefox, only the last property value in the list of properties is
passed to the function. Even if you click on an image at the top of
the list, still only the property number associated with the last list
item is passed.

The purpose of the script is to access data regarding the property
from an Object that was created prior. I use the property number
attribute to know which object item to access. I could also define
all the property data as attributes for each item in the list, but I
would still have the same problem: Firefox would only grab the values
from the last listed item. The purpose of this exercise is to not
have to go back to the server since the data has been transferred into
Javascript.

I can get the property number into a get variable in a hard coded
link, but this would go back to the server. I think that using any
self functions would still pose the same problem since I would still
have to place the property number in the function. Is there any way
to break the reference associated with this type of variable and
access it later in Firefox, to hard code it into a function for
subsequent clicking which IE and Netscape allows you to do?

Thanks for any help.

Feb 15 '07 #1
6 5800
scotty wrote:
I have a script that loops through an existing table list and prepares
each href element node to trigger a function when an image is
clicked. The function that will be run passes a property value to the
function upon clicking. The property value is one of the attributes
defined for each respective href in the property list.

In IE and Netscape, the property values pass correctly, but in
Firefox, only the last property value in the list of properties is
passed to the function. Even if you click on an image at the top of
the list, still only the property number associated with the last list
item is passed.
Show us the relevant JavaScript code and HTML. You might be using a
closure although it is not clear to me why you get different results
between IE and Firefox and particular between Netscape and Firefox.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 15 '07 #2
/* properties is an object that I carry through the functions that
does not
impact the problem. see below, and thanks */

function prepare_to_show_properties(properties)
{
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;

links=document.getElementsByTagName("a");
row_at='odd';
for(d=0;d<links.length;d++)
{
if(links[d].getAttribute('prop'))
{
links[d].onclick=function()
{
show_prop(this,properties);
return false;
}
links[d].onmouseover=function()
{
show_thumb(this);
}

link_parent=links[d].parentNode;
cell_parent=link_parent.parentNode;
if(row_at=='odd')
{
cell_parent.onmouseout=function()
{this.className="js_row_odd";}
row_at='';
}
else
{
cell_parent.onmouseout=function()
{this.className="js_row_even";}
row_at='odd';
}
cell_parent.onmouseover=function(){this.className= "js_mouseover";}
}
}
}

Thanks for the response:

show_prop(this,properties); > appears to be the offending line
for Firefox. "This" is the actual 'a' element and any of its
attributes which have been defined earlier:

a.setAttribute('prop', prop);

With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.prop)", or "alert(this.prop)", Windows and
Netscape both announce the correct property number no matter what line
I click on. Firefox does not. It announces the last property number
in the list no matter what link I click on. With IE I am able to stay
completely client. I have not been able to do so with Firefox.
Again, I have tried everything imaginable to try and get Firefox to
load a function as indicated. This is frustrating. By the way, the
show_thumb function above is also impacted by this
problem in Firefox as you might imagine.

One note, I rebuilt my page specifically in Firefox, making sure of
"well-formedness", etc. not thinking that this would help, but I
really like the resulting layout, and I end up with the same issue.

Scott

On Feb 15, 6:17 am, Martin Honnen <mahotr...@yahoo.dewrote:
scotty wrote:
I have a script that loops through an existing table list and prepares
each href element node to trigger a function when an image is
clicked. The function that will be run passes a property value to the
function upon clicking. The property value is one of the attributes
defined for each respective href in the property list.
In IE and Netscape, the property values pass correctly, but in
Firefox, only the last property value in the list of properties is
passed to the function. Even if you click on an image at the top of
the list, still only the property number associated with the last list
item is passed.

Show us the relevant JavaScript code and HTML. You might be using a
closure although it is not clear to me why you get different results
between IE and Firefox and particular between Netscape and Firefox.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Feb 15 '07 #3
On Feb 16, 12:59 am, "scotty" <scas...@cox.netwrote:
/* properties is an object that I carry through the functions that
does not
impact the problem. see below, and thanks */
Please don't top-post, reply below trimmed quotes.
>
function prepare_to_show_properties(properties)
{
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;

links=document.getElementsByTagName("a");

It is bad to allow variables that should be kept local to slip into
the global space, use var to keep them local. Also, the document
object has a links collection that you can use without resorting to
getElementsByTagName:

var links = document.links;

row_at='odd';
for(d=0;d<links.length;d++)
It is especially bad to allow counters to slip into the global space.

var row_at='odd';
for(var d=0; d<links.length; d++)

{
if(links[d].getAttribute('prop')) {
links[d].onclick=function() {
show_prop(this,properties);
This will create a closure back to the properties variable of the
prepare_to_show_properties() function. All onclick functions will have
a reference back to the same object.

I have re-formatted the following to be easier to read (for me) :-)
return false;
}
links[d].onmouseover=function() {
show_thumb(this);
}

link_parent=links[d].parentNode;
cell_parent=link_parent.parentNode;
if(row_at=='odd') {
cell_parent.onmouseout=function() {
this.className="js_row_odd";
}
row_at='';
} else {
cell_parent.onmouseout=function() {
this.className="js_row_even";
}
row_at='odd';
}
cell_parent.onmouseover=function(){this.className= "js_mouseover";}
}
}
}

Thanks for the response:

show_prop(this,properties); > appears to be the offending line
for Firefox. "This" is the actual 'a' element and any of its
attributes which have been defined earlier:

a.setAttribute('prop', prop);
It will be much easier to work out what's happening if you show the
function that sets the attribute, and what show_prop() is doing with
it. As Martin said, you are describing classic symptoms of an
unexpected closure that is making the value of some variable different
to what you think it will be.

Try posting a minimal, working example that demonstrates the error or
a link to a similarly minimal page.
>
With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.prop)", or "alert(this.prop)", Windows and
Netscape both announce the correct property number no matter what line
What property number? You didn't say what is in the "properties"
object. Apparently it was irrelevant, but now it is?

I click on. Firefox does not. It announces the last property number
in the list no matter what link I click on.
Yup, there's a closure somewhere that you don't expect. Why it is
behaving differently in IE/Netscape to Firefox is impossible to say at
this point.
--
Rob

Feb 15 '07 #4
On Feb 16, 8:43 am, "RobG" <r...@iinet.net.auwrote:
On Feb 16, 12:59 am, "scotty" <scas...@cox.netwrote:
[...]
With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.prop)", or "alert(this.prop)", Windows and
Netscape both announce the correct property number no matter what line

What property number? You didn't say what is in the "properties"
object. Apparently it was irrelevant, but now it is?
I click on. Firefox does not. It announces the last property number
in the list no matter what link I click on.
Since you appear to be setting non-standard properties on HTML
elements, try:

alert(this.getAttribute('prop'));
The closure issue might be a red herring.
--
Rob

Feb 15 '07 #5
On Feb 15, 4:42 pm, "RobG" <r...@iinet.net.auwrote:
On Feb 16, 8:43 am, "RobG" <r...@iinet.net.auwrote:
On Feb 16, 12:59 am, "scotty" <scas...@cox.netwrote:
[...]
With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.prop)", or "alert(this.prop)", Windows and
Netscape both announce the correct property number no matter what line
What property number? You didn't say what is in the "properties"
object. Apparently it was irrelevant, but now it is?
I click on. Firefox does not. It announces the last property number
in the list no matter what link I click on.

Since you appear to be setting non-standard properties on HTML
elements, try:

alert(this.getAttribute('prop'));

The closure issue might be a red herring.

--
Rob
I really appreciate your response and advice. Thank you. I cleaned
things up a bit with some of your suggestions.

Also, I wanted to let you know that you solved the problem with the
following:

this.getAttribute('prop')

Instead of "this", I have to use the name I gave the function
argument, so used:
var prop = property.getAttribute('prop');

Thank you very much.

Scott

Feb 16 '07 #6
On Feb 15, 3:42 pm, "RobG" <r...@iinet.net.auwrote:
On Feb 16, 8:43 am, "RobG" <r...@iinet.net.auwrote:

The closure issue might be a red herring.
I totally agree, Rob--the closure might not be the source of the bug,
and without being able to actually see the difference, it's hard to
say. (I also agree with all your other comments on this code.)

On the other hand, even if the closure doesn't cause the bug in
question, this particular closure will produce a circular DOM-JS link,
leading to a memory leak in IE6.

Simpler example:

(function(){
var x=document.getElementsByTagName('a')[0];
x.onclick=function() {
// ...
};
})();

In this case, the leaked nodes are cell_parent and link_parent, since
there is never a reference directly to the link itself. Setting
cell_parent to null at the end of each loop would break the cycle and
prevent the leak. (Oh, how I long for the day when IE6 falls into the
void of time like NN4 and IE3 and we no longer have to cater to faulty
garbage-collection mechanisms.)

--i

Feb 16 '07 #7

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

Similar topics

12
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Here is the code i'm using: <head> <script language='JavaScript'>...
2
by: Cy | last post by:
Hi, I have a menu that toggles correctly in IE but is failing in FireFox V.1 and Netscape 7.1. The FireFox JavaScript Console is returning the following error; Error:...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
3
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
3
by: Brian A | last post by:
I have written some Javascript that works perfectly in Firefox - no errors reported. The dreaded MSIE 6 produces nothing but a long line of 'undefinedundefinedundefinedundefinedundefined ....'...
3
by: kj | last post by:
How can a script send (non-fatal) warnings and other such messages to Firefox's JavaScript console? Thanks! kj -- NOTE: In my address everything before the first period is backwards; and...
12
by: tim | last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus in firefox. On my sub3 menus i have more than one line of text in some places. firefox does not recognise that there is more...
4
by: MartinRinehart | last post by:
I've written a short article explaining closures in JavaScript. It's at: http://www.martinrinehart.com/articles/javascript-closures.html I think I've understood. I look forward to your...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.