Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Cross-domain messaging between iframes with immediate response

Retrieving a value, without using a callback

3:53 pm on Aug 5, 2013 (gmt 0)

Full Member

10+ Year Member

joined:June 29, 2005
posts: 216
votes: 0

Bear with me, I've been working on this solidly for 2 weeks now, so I'll do my best to explain it.

In all of this, I am working within specific limitations that cannot be changed. It might seem odd, but there it is!

So, I have a HTML page at example1.com/host.html.

This page contains an iframe pointing to example2.com/contents.html.

That's the structure I've got to work with, and that's fine. What I need, though, is for contents.html to retrieve some data from host.html via javascript.

Now, I realise this is the classic cross-domain javascript problem, and there are thousands of resources out there to address that. The specific problem I'm having is that I can communicate from example2.com to example1.com fine, but I can't get a response immediately back.

What I need to do is provide a javascript method in example2.com called GetValueFromExample1(). From example2.com, this method will call another method on example1.com. It must then return the value it finds.

It's this last bit I can't get my head around. All the solutions I can find just do 1-way communication. Sure, they can respond later via a callback, but the GetValueFromExample1 method has already finished by then, and I've got to return something.

I will have no control over the javascript that will originally call this GetValueFromExample1 method, so I can't do anything different with that.

Just so you know my last 2 weeks haven't been in vain, I've got a solution that works fine in FireFox and IE, but not Chrome. I've done that by making a list of requests in example2.com, and when GetValueFromExample1 runs, it adds the request to that list, calls example1.com (using a third-party cross domain library), then sits in a loop until the request is updated with the response in the list. When example1.com has received the request, it then makes a call back to example2.com with the response, which updates the list.

(Got that?)

Like I said, this actually works in IE and Firefox, but it appears as though Chrome only runs the javascript of the initial frame (contents.html), and so ends up in an infinite loop, and the call to example1.com never executes. If I change it so the method doesn't sit in the loop, and returns a dummy value, then the message to example1.com goes through. But by that point it's too late, as I can't return the response then.

Has anyone got any suggestions, or am I barking up the wrong tree?

I'm happy to completely change how it's working, as I'm feeling this is getting very hacky now, but I can't seem to think of any other options.

Just to clarify, in example1.com, I can only run javascript, but I have access to server-side stuff in example2.com, if that helps.