Forum Moderators: open
function myFunction(url, myVariable)
{
new Ajax.Request(url, {method:'post', parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
});
} I want to be able to use the myVariable variable that's part of the function line in the onSuccess function, but it seems that the scope doesn't work. When I try to use it, the variable is undefined.
Is there a way to use it? I'm sure I'm missing something easy.
function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
}
);
}
Should be:
function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",
onSuccess:function()
{
alert(myVariable);
}
}
);
}
Don't know if that's your problem or not... no time to look close right now.
And thank you for calling it a "nameless" function. I've been looking for information but didn't know that term so I was having a hard time finding anything.
[edited by: Nutter at 6:38 pm (utc) on Jan. 14, 2008]
window.myVar . That should be able to be referenced to anywere. I think your code is failing because of a syntax error though
function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters="value=somevalue",// JSON string so = isn't valid
onSuccess:function()
{
alert(myVariable);
}
}
);
}
function myFunction(url, myVariable)
{
new Ajax.Request
(
url,
{
method:'post',
parameters:'value=somevalue',
onSuccess:function()
{
alert(myVariable);
}
}
);
}
Also if your interested in "nameless" functions they may also be referfed anonymous functions.
Quick note on scope
class.prototype={
hello:"hi" //hello belongs to class.prototype
foo:function (){
var bar="foo" //bar belongs to class.prototype.foo
global // variable not defined in class.prototype.foo assumed to be a global
this // refers to class.prototype or an object constructed by new class();
}
}
And thank you for calling it a "nameless" function. I've been looking for information but didn't know that term so I was having a hard time finding anything.
[edited by: Fotiman at 5:00 pm (utc) on Jan. 15, 2008]