8

I have an iframe in my web app and I need to get its current url from the parent document (also when the user navigates the frame and change the original source url).

Url is needed simply to social share it.

As a cross-domain scenario, I don't own the child document (its a remote domain).

I am aware of the same-origin-policy that prevents cross domain access from parent document to child one, but I'm looking for a solution by any creative mean possible - there has to be a way around.

Fallouturama
  • 109
  • 1
  • 1
  • 9
  • Similar epascarello, he asks also for a way with no iframes. The suggestions are not all cross browser compliant, they don't work correctly -said by the guy- and is a bit outdated. – Fallouturama Jan 07 '12 at 00:58
  • Also he makes more emphasis on a server side solution, I prefer to find a client side one. – Fallouturama Jan 07 '12 at 01:05
  • You can not get the url, there is this thing called SECURITY that prevents it. If you want to be "social" you will need to develop a browser plug-in that can get around it. Other than that, you can not read it. – epascarello Jan 07 '12 at 02:30
  • I need to be social or my project is pointless. Is posible to create then a cross-domain proxy allowing the parent doc to retrieve the url throught Javascript and also keeping the user navigation inside the iframe by forcing all urls to pass through the proxy? what do you think about Flash and Javascript manipulated urls in webpages.. Can this proxy handle those? – Fallouturama Jan 09 '12 at 20:33
  • Im afraid browser plugins are not an option, I need a cross browser solution.Proxys has been made before to accelerate, filter or manipulate somehow user data. Amazon's Silk, Opera Mini and others browsers had used that philosophy before in web navigation, why users shouldn't trust my site if I explain them legally the terms of use? – Fallouturama Jan 09 '12 at 20:38
  • Nope, not even being mendocino helped. – Fallouturama Nov 20 '14 at 02:10
  • Do you control the "framed" content in any way? Can you insert a "plug-in" to the framed content? If so, you could use window. postMessage() to send the current URL back to the parent. – Alain Nisam Feb 21 '16 at 01:12

1 Answers1

4

If you don't control the iFrame content, then the only solution is to create a proxy that injects the following line of code into every page.

window.postMessage(window.location,'*');

Then on the parent page you can listen for the message.

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
  console.log(event.data);
}
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70