variable scope

4 views
Skip to first unread message

kenez...@gmail.com

unread,
May 3, 2007, 6:10:56 AM5/3/07
to greasemonkey-users
Hi. I have a piece of code:
for(i=0; i<TRno; i=i+1)
{
var tnindex=4+i*6;
var mhoindex=8+i*6;

TRname =
rowelements[tnindex].childNodes[1].childNodes[1].childNodes[0].nodeValue;
MHO =
rowelements[mhoindex].childNodes[13].childNodes[1].childNodes[0].nodeValue;

if(MHO=='something')
{
var url='http://some.link';
}
else if(MHO=='smthelse')
{
var url='http://some.other.link';
}


var newlink=document.createElement('a');
newlink.target = '_blank';
newlink.innerHTML='>>';
newlink.href=url+TRname;
rowelements[tnindex].childNodes[1].appendChild(newlink);
}

The problem is that when I want to test the value of the MHO variable
in the if statements, its value is undefined. I thought that I should
have no scope problems, but there it is. Anyone have any idea?

Thanks,
Tamas

RodMcguire

unread,
May 3, 2007, 7:58:46 AM5/3/07
to greasemonkey-users
On May 3, 6:10 am, "kenez.ta...@gmail.com" <kenez.ta...@gmail.com>
wrote:

> The problem is that when I want to test the value of the MHO variable
> in the if statements, its value is undefined. I thought that I should
> have no scope problems, but there it is. Anyone have any idea?

I don't think you have a scope problem. It may be that you are trying
to get .nodeValue from some object where it is undefined. E.g;
('abc'.nodeValue == undefined)

Kenéz Tamás

unread,
May 3, 2007, 8:05:20 AM5/3/07
to greasemon...@googlegroups.com
Hi,

The thing is, that I checked before, and just before the if statement,
the MHO variable has the correct value, and is NOT undefined.
Any ideas?

BR,
Tamas


--
May the road rise to meet you.
May the wind be always at your back.
May the sun shine warm upon your face.
May the rains fall soft upon your fields.
And until we meet again,
May God hold you in the hollow of His hand.
(Irish blessing)

RodMcguire

unread,
May 3, 2007, 8:38:05 AM5/3/07
to greasemonkey-users

try adding this code before the 'if'.

nMHO =
rowelements[mhoindex].childNodes[13].childNodes[1].childNodes[0];
alert(nMHO + '\n' + nMHO.nodeValue);

Kenéz Tamás

unread,
May 3, 2007, 8:48:31 AM5/3/07
to greasemon...@googlegroups.com
I get the following in the alert box:
[object XPCNativeWrapper [object Text]]
value

where "value" is exactly what I expect to be its value.

On 5/3/07, RodMcguire <mcg...@telerama.com> wrote:
>
>

Anthony Lieuallen

unread,
May 3, 2007, 10:12:40 AM5/3/07
to greasemon...@googlegroups.com
On 5/3/2007 6:10 AM, kenez...@gmail.com wrote:
> Hi. I have a piece of code:

Unless there's a specifically good reason not to, it's always much
better, when asking for help, to give the WHOLE script, and the WHOLE
page it is being applied to. For reada- and usa-bility, links for both
are often best.

BD

unread,
May 3, 2007, 4:32:48 PM5/3/07
to greasemon...@googlegroups.com
If MHO is undefined, then
rowelements[mhoindex].childNodes[13].childNodes[1].childNodes[0].nodeValue
is also undefined.

That should be your problem. So either your mhoindex value is incorrect
and/or being calculated out of bounds, or your line of reference from
childNodes[13].... is undefined somewhere.

--
Which conspiracy theory do you believe?
The Government's Official Conspiracy Theory Story,
or one of the alternative conspiracy theories?
Demand Truth and Justice! http://www.Chico911Truth.org/

Wenzel.P...@googlemail.com

unread,
May 3, 2007, 6:38:24 PM5/3/07
to greasemonkey-users
Hi,

On May 3, 11:10 am, "kenez.ta...@gmail.com" <kenez.ta...@gmail.com>
wrote:

> rowelements[tnindex].childNodes[1].appendChild(newlink);

if I understand your script right you are skipping rows in a table.
The way you coded it you will most likely ending with:

i < TRno && 8+i*6 > TRno

What means that for one of your last iterations your calculated offset
"mhoindex" will be bigger then TRno. Given that TRno is the maximum
number of rows in the table accessing that row will fail.

If you work with tables you may want to take a look at the rows and
cells properties.

cell = table.rows[i].cells[j];
if(cell.textContent.match(/awfull/)){

}

As you seen by all those replies ppl have a hard time to understand
your code. Even as you (seam to) do something quite basic like walking
over a table. For my eyes your coding style is rubbish. You could
start to do better by changing i=i+1 to i++.

If the web page you want to grease is on the WWW a link to sayed page
with maybe a hint to what element you want to change will always be a
good idea.

Neil Greenwood

unread,
May 8, 2007, 6:41:06 AM5/8/07
to greasemon...@googlegroups.com
On 03/05/07, kenez...@gmail.com <kenez...@gmail.com> wrote:
> [snip]

>
> if(MHO=='something')
> {
> var url='http://some.link';


I think your problem is here. url is defined as a variable inside the
if block (and the else block as a separate variable...

> }
> else if(MHO=='smthelse')
> {
> var url='http://some.other.link';

... here).

> }
>
>
> var newlink=document.createElement('a');
> newlink.target = '_blank';
> newlink.innerHTML='>>';
> newlink.href=url+TRname;

Then you try to use it here when it's out of scope.


If this snippet is representative of the real script, you should
change it to something like the following:
var url;
if(MHO=='something')
{


url='http://some.link';
}
else if(MHO=='smthelse')
{

url='http://some.other.link';
}


If this isn't the problem, as someone else requested you need to
include the full code and a URL that you're trying to use it on.


HTH
Hwyl,
Neil.

Aaron Boodman

unread,
May 8, 2007, 12:19:03 PM5/8/07
to greasemon...@googlegroups.com
On 5/8/07, Neil Greenwood <neil.green...@gmail.com> wrote:
I think your problem is here. url is defined as a variable inside the
if block (and the else block as a separate variable...

...

Then you try to use it here when it's out of scope.

JavaScript does not have block scope (all variables are either function or global level) so it must be something else.  Kenez, post the original and we'll be able to help better.

- a


BD

unread,
May 8, 2007, 2:11:10 PM5/8/07
to greasemon...@googlegroups.com
If you fixed it, please post and let us know what the fix was. Thanks.

Kenéz Tamás

unread,
May 9, 2007, 4:54:58 AM5/9/07
to greasemon...@googlegroups.com
Sorry for the delayed answer, everyone, and thanks for all your
efforts on helping me!

It finally works! The thing that fixed it was modifying the check
condition in the if's from MHO=='something' to
MHO.textContent.match('something'). It still puzzles me how this
solved it, but the important thing is that it works. Thanks again for
all your help,

BR,
Tamas

Reply all
Reply to author
Forward
0 new messages