React项目中使用iframe,IE11同源地址内容不加载

2,492 阅读2分钟

遇到的问题

因满足需求,打开新页签需要用iframe嵌入同源地址下另一个网站的内容,切换页签更换token获取不同客户信息。

功能开发完成后在chrome、firefox、edge等浏览器下都能正常显示。通过IE11打开iframe空白。开发人员工具查看iframe的body为空。

<iframe id="iframe" class="customer" src="http://localhost:8081/#/root/customer/" height="600" scrolling="auto" name="iframe">
    <html>
      <head></head>
      <body></body>
    </html>
</iframe>

分析问题

从dom结构来看,IE没有加载该URL的内容。 src的地址存在问题,就尝试更换不同地址看能否显示,尝试的结论:

http://localhost:8081/#/..... (不显示)
./#/root/customer/(不显示)
http://localhost:8081/#/root/customer/?time=1579255141031 (不显示)
document.getElementById('iframe').src=`http://localhost:8081/#/...`(不显示)
将src网址更改为其他任何网站(显示)
创建html静态页面,src指向该静态页面 (显示)

解决方案

从尝试不同src过程来看,同源地址下的动态页面打开时IE并未加载它,IE一直是个坑,最近微软发布新的浏览器"Microsoft Edge",感觉好用不少,什么时候客户不再使用IE6~11的浏览器啊。

既然HTML静态页面可以打开,如果在静态页面跳转到指定页面会不会显示,实验结果是正常显示,问题终于解决了。

html静态页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript">
    document.location = "//"+ location.host + '/#/root/customer/'
    </script>
</head>
<body>
</body>
</html>