如何从iframe跨域获取内容
问题描述:
有没有办法通过php / jQuery或JavaScript从iframe获取内容?当用户点击iFrame中的链接及其标题时,我想获取网址。
Is there any way to get contents from iframe via php/jQuery or JavaScript? I want to get url when user click on a link in iFrame and its title also.
答
来自任意网址?否。
来自您控制的网址?是的,在现代浏览器中。
From URLs you control? Yes, in modern browsers.
要点击的文档应该使用 postMessage 将您感兴趣的任何数据发送到需要监听消息事件的其他文档。
The document being clicked in should use postMessage to send whatever data you are interested in to the other document which needs to listen for a message event.
top.postMessage({ url: location.href }, "*");
和
window.addEventListener("message", receiveMessage, false);
function receiveMessage(evt) {
alert(event.data.url);
}