Your first example doesn't work because getJSON doesn't return the JSON data
as its return value; it *calls a function* and passes the data as an
argument to that function.

Remember that getJSON is asynchronous. The call to getJSON returns before
the data is even downloaded. Any time you have asynchronous operations in
JavaScript, you'll do them through a callback function like this. (There is
a synchronous option for getJSON, but you really don't want to use it - it
causes serious problems.)

Your getJSON callback function *could* store the data in a variable in the
parent page:

    $.getJSON( url, function( data ) {
        top.myData = data;
    });

But there's a flaw in this approach. How does the code in the parent page
know when the data is ready? It doesn't.

Instead, you should provide a function in the parent page and *call* it from
the getJSON callback:

    $.getJSON( url, function( data ) {
        top.dataIsReady( data );
    });

where the parent page has this global function:

    function dataIsReady( data ) {
        // ...
    }

In fact, if calling that function is the only thing your callback does, you
could just use the function directly as the callback:

    $.getJSON( url, top.dataIsReady );

-Mike

On Fri, Dec 4, 2009 at 8:29 AM, m.ugues <m.ug...@gmail.com> wrote:

> I tried this way:
>
> http://pastie.org/727632
>
> The div is appendend but the javascript declaration not:
>
> Any idea?
>
> Kind regards
> Massimo
>
> On 4 Dic, 16:13, "m.ugues" <m.ug...@gmail.com> wrote:
> > Hallo all.
> > I would like to save the response of an AJAX call in a variable
> > declared in the document ready in parent page.
> >
> > Imagine this piece of code
> >
> > http://pastie.org/727450
> >
> > The document ready lives in an iframe so I would like to save in the
> > parent page the var myVariable, not to load it every time with a
> > server-side call.
> >
> > Is there any way to inject in the parent the javascript variable?
> >
> > Kind regards
> >
> > Massimo
>

Reply via email to