当前位置:   article > 正文

iframe嵌入页面跨域通信_iframe跨域问题 嵌入别人的网站

iframe跨域问题 嵌入别人的网站

在项目中可能会通过iframe直接将另一个页面嵌入进来,某些场景下还可能会进行一些消息的传递通信。之前做过一次,就是我们开发的主系统,然后下面还有很多子系统,子系统都是通过iframe嵌入进来的,然后为了实现某些需求,需要将token传递给子系统,拱子系统使用,但是当时一直忙着开发,忘记记录了,所以这篇算是补上吧

嵌入进来的页面的window跟我们现在的window已经不是同一个了,更不是同一个document。

然后跨域通信的话,父窗口向子窗口我们通过 iframe.contentWindow.postMessage 发送消息;子窗口向父窗口我们通过 window.parent.postMessage 发送消息;然后接受的话都是监听window的message事件,如下:

otherWindow.postMessage(message, targetOrigin, [transfer]);
  • message

         是我们发送的信息,一般我会传个JSON,也可以是个对象、字符串。。

  •  targetOrigin

        表示目标窗口的源,协议+域名+端口号,如果设置为“ * ”,则表示可以传递给任意窗口。 

第三个我暂时还没用过,感兴趣的伙伴可以自行找找看奥 

父窗口向子窗口发送消息

  1. iframe.contentWindow.postMessage(JSON.stringify({
  2. type: 'www',
  3. ctx: '给你消息,收到了嘛?'
  4. }), '*')

子窗口向父窗口发送消息 

  1. window.parent.postMessage(JSON.stringify({
  2. type: 'yes',
  3. ctx: '我收到您发送的消息了!'
  4. }), '*')

接受消息 

包括父窗口发送给子窗口的和子窗口发送给父窗口的,都一样,都是通过监听window的message事件 

  1. // 接受消息
  2. window.addEventListener('message', receiveMessage, false)
  3. function receiveMessage(event) {
  4. const msg = JSON.parse(event.data)
  5. console.log(msg, '----->>>')
  6. }

测试的完整代码 

父窗口

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <style>
  11. * {
  12. padding: 0;
  13. margin: 0;
  14. box-sizing: border-box;
  15. }
  16. .content {
  17. width: 100vw;
  18. height: 100vh;
  19. position: relative;
  20. }
  21. .content button {
  22. position: absolute;
  23. top: 150px;
  24. left: 50%;
  25. transform: translateX(-50%);
  26. }
  27. .iframe {
  28. width: 500px;
  29. height: 500px;
  30. border: 1px solid #ccc;
  31. border-radius: 6px;
  32. position: absolute;
  33. top: 50%;
  34. left: 50%;
  35. transform: translate(-50%, -50%);
  36. }
  37. </style>
  38. <div class="content">
  39. <button id="btn">SEND</button>
  40. <div class="iframe">
  41. <iframe id="my-iframe" src="./iframe.html" style="width: 100%;height: 100%;" frameborder="0"></iframe>
  42. </div>
  43. </div>
  44. <script>
  45. const $ = name => document.querySelector(name)
  46. // 发送消息到子窗口
  47. $('#btn').onclick = function(e) {
  48. $('#my-iframe').contentWindow.postMessage(JSON.stringify({
  49. type: 'www',
  50. ctx: '给你消息,收到了嘛?'
  51. }), '*')
  52. }
  53. // 接受子窗口的消息
  54. window.addEventListener('message', receiveMessage, false)
  55. function receiveMessage(event) {
  56. const msg = JSON.parse(event.data)
  57. console.log(msg, '----->>>')
  58. }
  59. </script>
  60. </body>
  61. </html>

子窗口 (iframe.html)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <style>
  11. * {
  12. padding: 0;
  13. margin: 0;
  14. box-sizing: border-box;
  15. }
  16. .iframe-content {
  17. width: 100vw;
  18. height: 100vh;
  19. background-color: #f5f5f5;
  20. text-align: center;
  21. line-height: 100vh;
  22. }
  23. </style>
  24. <div class="iframe-content">
  25. <span>我是Iframe</span>
  26. <button id="iframe-btn">SEND</button>
  27. </div>
  28. <script>
  29. // 接受父窗口的消息
  30. window.addEventListener('message', receiveMessage, false)
  31. function receiveMessage(event) {
  32. const msg = JSON.parse(event.data)
  33. console.log(msg, '----->>>')
  34. }
  35. // 10s后发送消息到父窗口
  36. setTimeout(() => {
  37. window.parent.postMessage(JSON.stringify({
  38. type: 'yes',
  39. ctx: '我收到您发送的消息了!'
  40. }), '*')
  41. }, 5000)
  42. </script>
  43. </body>
  44. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/774426
推荐阅读
相关标签
  

闽ICP备14008679号