Getting iframe current src url in cross domain [duplicate]

微笑、不失礼 提交于 2019-12-30 08:49:31

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/8766281/getting-iframe-current-src-url-in-cross-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!