[pmrpc] How to call a procedure on window.top

79 views
Skip to first unread message

orensol

unread,
Apr 26, 2010, 10:03:56 AM4/26/10
to pmrpc
Hi

When I want to call a procedure on the top window, should I use

destination: window.top

Because it seems it doesn't work for me this way.


--
Subscription settings: http://groups.google.com/group/pmrpc/subscribe?hl=en

Ivan Žužak

unread,
Apr 26, 2010, 10:15:31 AM4/26/10
to pm...@googlegroups.com, or...@triond.com
Hi,

Yes, you should use window.top. But as you say, it's not working for
you for some reason. Can you e-mail me your code (or relevant
snippets) so we can try to find out what's wrong? Thanks.
Also - which browser and version are you using?

Ivan

orensol

unread,
Apr 26, 2010, 10:22:55 AM4/26/10
to pmrpc
Hi Ivan,

I'm using Firefox 3.6. In firebug, when I put a breakpoint and watch
the variables, I see that destination is of type Window but undefined,
so postMessage isn't being called.

The code i'm using is taken straight from the API docs.
The window top has this:

var publicProcedureName = "HelloPMRPC";
var procedure = function (alertText) { alert(alertText); return
"Hello!"; };

pmrpc.register( {
"publicProcedureName" : publicProcedureName,
"procedure" : procedure
} );


and the iframe (from different domain) has this:

var parameters = {
destination : window.top,
publicProcedureName : "HelloPMRPC",
params : ["Hello world from pmrpc client!"],
onSuccess : function(returnObj) {alert(returnObj.returnValue);},
onError : function(statusObj) {alert(statusObj.description);},
retries : 15,
timeout : 1500,
destinationDomain : "*"
};

pmrpc.call(parameters);


Any ideas?
Thanks,
Oren

Ivan Žužak

unread,
Apr 26, 2010, 11:11:22 AM4/26/10
to pm...@googlegroups.com, or...@triond.com
Hi Oren,

I tried to reproduce the environment here:

http://dl.dropbox.com/u/1559679/parent.html (parent window that
registers a function)
http://dl.dropbox.com/u/1559679/iframe.html (child iframe that calls
the function using window.top)

This example is working for me in both Chromium and Firefox. Can you
try this and let me know if it works? If it does work - try to find
the difference between this code and the one you sent me earlier
(could by a typo or something -- e.g. try deleting the newline between
"return" and "hello!" in the top window code).

Also, if it works - did you run the code you sent me earlier from your
local drive? If so, please read
http://code.google.com/p/pmrpc/wiki/PmrpcApiDocs#Important_notes.
Basically, due to some weird security policy implemented in Firefox
for local files - pmrpc won't work for local files in Firefox.

Cheers,
Ivan

orensol

unread,
Apr 27, 2010, 4:12:32 AM4/27/10
to pmrpc
Hi Ivan,

I just tried your example and it works for me.

My code isn't running from local drive, so I am looking for
differences. It seems there's no typo, there's no js error. I just
after a while get "Application error. Destination unavailable".

I thought the difference was maybe related to the fact that I included
also json2.js, but removing it doesn't fix it.

Another difference is that pmrpc.js isn't statically written in the
head element of my document, it is loaded dynamically when the DOM is
ready. I didn't look very thoroughly through the pmrpc.js code, so i'm
not sure if it makes a difference.

And the last difference is that my iframe comes from a different
domain than that of window.top, and yours doesn't. Can you try an
example where the iframe is from a different domain? Maybe there's
some same-origin-policy limitation here?

orensol

unread,
Apr 27, 2010, 4:37:44 AM4/27/10
to pmrpc
Ivan,

I tried my own code on an iframe from the same domain and it works.

So there's definitely a same-origin-policy problem here. I am not sure
where, but the iframe from the different domain probably can't get the
handle to window.top.

Any ideas how to work around?

Ivan Žužak

unread,
Apr 27, 2010, 5:53:09 AM4/27/10
to pm...@googlegroups.com, or...@triond.com
Hi Oren,

I updated the example from before, now the iframe and the parent page
are on different domains. Works on Chromium and Firefox for me. Please
try it and let me know if it still doesn't work:
http://dl.dropbox.com/u/1559679/parent.html (parent window that
registers a function)
http://pmrpc.googlecode.com/svn/trunk/testing/iframe.html (child
iframe that calls the function using window.top)

From this, I'd say there is no cross-origin issue here, must be something else.

Also, I do believe there is a "bug" in the line:
var procedure = function (alertText) { alert(alertText); return "Hello!"; };
In your code, there is a newline between return and "Hello!" which
causes my code to break, and when I remove it - everything works fine.

As for the dynamic loading of pmrpc - this could also mess something
up, but I don't think it is since you're not getting a "pmrpc is not
defined" error, but something that suggests that pmrpc has loaded
fully before your application logic started executing.

Let me know,
Ivan

orensol

unread,
Apr 27, 2010, 7:25:59 AM4/27/10
to pmrpc


On Apr 27, 12:53 pm, Ivan Žužak <izu...@gmail.com> wrote:
> Hi Oren,
>
> I updated the example from before, now the iframe and the parent page
> are on different domains. Works on Chromium and Firefox for me. Please
> try it and let me know if it still doesn't work:http://dl.dropbox.com/u/1559679/parent.html(parent window that
> registers a function)http://pmrpc.googlecode.com/svn/trunk/testing/iframe.html(child
> iframe that calls the function using window.top)
>
> From this, I'd say there is no cross-origin issue here, must be something else.
>

It seems to work for me as well, however sometimes (usually after a
ctrl+f5 refresh) after the first alert ("Hello world from pmrpc
client!") I get the "Application error. Destination unavailable."
alert instead of the expected "Hello!".

Sometimes I do get the "Hello!" though.

In my code it works as expected when the iframe is from same domain,
but doesn't work at all when the iframe is from a different domain.

> Also, I do believe there is a "bug" in the line:
> var procedure = function (alertText) { alert(alertText); return "Hello!"; };
> In your code, there is a newline between return and "Hello!" which
> causes my code to break, and when I remove it - everything works fine.
>

The line break here is google groups adding the break, in the code
it's without the break.

I am not sure how I can proceed from here. When debugging with
firebug, it seems that when iframe is not from same domain I have
destination as "Window undefined" but when on same domain, destination
is "Window 377" (with 377 being the end of the url "blah.com/post/
377").

Also, it seems like the ping request post message is called but never
happens.

Ivan Žužak

unread,
Apr 27, 2010, 7:42:00 AM4/27/10
to pm...@googlegroups.com, or...@triond.com
On Tue, Apr 27, 2010 at 13:25, orensol <or...@triond.com> wrote:
> It seems to work for me as well, however sometimes (usually after a
> ctrl+f5 refresh) after the first alert ("Hello world from pmrpc
> client!") I get the "Application error. Destination unavailable."
> alert instead of the expected "Hello!".
>
> Sometimes I do get the "Hello!" though.
>
> In my code it works as expected when the iframe is from same domain,
> but doesn't work at all when the iframe is from a different domain.

OK, this rules out the cross-origin issue. From the looks of things,
I'd say there's a strange timing issue happening due to (possibly)
dynamic loading of pmrpc and different load times of windows/frames
from different domains.
Is there any chance you can give me the exact code you're trying to
get working so that I can debug the exact case? (without the
application logic, of course). I myself cant reproduce the error by
hitting ctrl-f5 or ctrl-R so it must be something else.

Also, can you try adding the pmrpc.call inside a setTimeout so that it
gets called after 1-2 seconds after it does now?

> I am not sure how I can proceed from here. When debugging with
> firebug, it seems that when iframe is not from same domain I have
> destination as "Window undefined" but when on same domain, destination
> is "Window 377" (with 377 being the end of the url "blah.com/post/
> 377").

That's strange... for me, it always returns a valid Window reference,
no matter if it on the same domain or not.

Ivan
Reply all
Reply to author
Forward
0 new messages