473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5820
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(pro perties)
{
if(!document.ge tElementsByTagN ame) return false;
if(!document.ge tElementById) return false;

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

link_parent=lin ks[d].parentNode;
cell_parent=lin k_parent.parent Node;
if(row_at=='odd ')
{
cell_parent.onm ouseout=functio n()
{this.className ="js_row_odd ";}
row_at='';
}
else
{
cell_parent.onm ouseout=functio n()
{this.className ="js_row_even"; }
row_at='odd';
}
cell_parent.onm ouseover=functi on(){this.class Name="js_mouseo ver";}
}
}
}

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.pro p)", or "alert(this.pro p)", 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...@yaho o.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.ne twrote:
/* 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(pro perties)
{
if(!document.ge tElementsByTagN ame) return false;
if(!document.ge tElementById) return false;

links=document. getElementsByTa gName("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
getElementsByTa gName:

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=functi on() {
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=fu nction() {
show_thumb(this );
}

link_parent=lin ks[d].parentNode;
cell_parent=lin k_parent.parent Node;
if(row_at=='odd ') {
cell_parent.onm ouseout=functio n() {
this.className= "js_row_odd ";
}
row_at='';
} else {
cell_parent.onm ouseout=functio n() {
this.className= "js_row_eve n";
}
row_at='odd';
}
cell_parent.onm ouseover=functi on(){this.class Name="js_mouseo ver";}
}
}
}

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.pro p)", or "alert(this.pro p)", 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.ne twrote:
[...]
With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.pro p)", or "alert(this.pro p)", 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.getA ttribute('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.ne twrote:
[...]
With IE, the function show_prop ushers in both objects just fine.
When I then use "alert(prop.pro p)", or "alert(this.pro p)", 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.getA ttribute('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.getAttribu te('prop')

Instead of "this", I have to use the name I gave the function
argument, so used:
var prop = property.getAtt ribute('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.getE lementsByTagNam e('a')[0];
x.onclick=funct ion() {
// ...
};
})();

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
25139
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'> <!-- var a=(screen.width-750)/2;
2
7418
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: document.getElementById(showDiv) has no properties. Any advice would be much appreciated. Here is the snippet of applicable code; <script language="javascript">
22
4583
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and...
3
3343
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 cut/paste it into 2 files and run it to see what I mean. ###############menu.html############### <html> <body> <script type="text/javascript">
16
3207
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 me. I was doing research on JavaScript inheritance recently (simplifying it in particular) and after reading 10.1.1, 10.1.3 and some other sections...
3
2293
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 ....' Each 'undefined' represents a text node or element I have interted into the DOM. As far as I can see I have declared all variables and asigned values...
3
33367
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 the last period, and everything after it, should be discarded.
12
2570
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 than one line and the text simply overlaps the sub-menus below it. I thought i had got around this by placing empty 'spacers' like so; ...
4
1839
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 constructive critique.
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.