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!

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮