douye2111 2013-07-19 11:54
浏览 67
已采纳

跨域/协议iframe,可访问服务器和现代功能

I am trying to embed an iframe into an https site. The site being embedded can only use the http protocol.

I'm getting a console error that the frame was blocked because "The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match".

Once I have the frame working I then want to run code on the embedded site. I think using the postMessage framework should do this for me, but I don't want the code that receives those messages on the embedded page to always be included, so want to use Javascript on the parent site to inject the JS file. Trying to use contentDocument but getting an error that this is null.

There is no requirement to support old browsers, so all the new HTML5 stuff is fine so long as IE10/Chrome/Safari/Firefox latest will run it. I have full access to both servers so editing PHP/Apache headers is fine.

  • 写回答

1条回答 默认 最新

  • dtn43447 2013-07-19 16:21
    关注

    To answer my own question, after several more hours struggle, it turns out it isn't going to be possible to inject a JS file when the parent is https and the child is http.

    However, once I gave up on this dream, it was quite easy!

    Before loading the iframe, I make a CORS AJAX request from website A to the site I want to display in the iframe (website B), to a PHP file that checks the HTTP_ORIGIN value to make sure the correct server is accessing. If it is, it sets a session variable on website B. Then, when I load website B from website A in the iframe, it uses the session value to know it's OK to include the javascript file I was going to inject onto website B.

    After that, the postMessage stuff all worked fine over different protocols with no extra work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog