test.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>测试页</title>
  7. <style>
  8. body {
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. height: 100vh;
  13. margin: 0;
  14. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  15. background-color: #f5f5f5;
  16. }
  17. .container {
  18. text-align: center;
  19. padding: 40px;
  20. }
  21. button {
  22. padding: 15px 40px;
  23. font-size: 16px;
  24. background-color: #1890ff;
  25. color: white;
  26. border: none;
  27. border-radius: 4px;
  28. cursor: pointer;
  29. transition: background-color 0.3s;
  30. }
  31. button:hover {
  32. background-color: #40a9ff;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <h2>测试页面(模拟外部跨域页)</h2>
  39. <p>模拟 https://w7.12123wzcx.com 行为</p>
  40. <br>
  41. <button id="closeBtn">关闭并传参给父页面</button>
  42. </div>
  43. <script>
  44. document.getElementById('closeBtn').addEventListener('click', function () {
  45. // ====== postMessage 方案:不刷新父页面 + 跨域支持 ======
  46. if (window.opener) {
  47. // 通过 opener 向父页面(visitorEntry)发送消息
  48. window.opener.postMessage(
  49. {
  50. type: 'addUsersuccess',
  51. value: '1',
  52. extraData: {
  53. userId: '12345',
  54. userName: '张三',
  55. cardId: 'card_001',
  56. timestamp: Date.now()
  57. // ... 任意多个参数
  58. }
  59. },
  60. '*'
  61. );
  62. console.log('已通过 postMessage 发送参数 addUsersuccess=1');
  63. // 关闭当前窗口(不影响父页面状态)
  64. window.close();
  65. // 如果 close 无效,提示用户
  66. setTimeout(function () {
  67. if (document.visibilityState === 'visible') {
  68. alert('参数已发送,请手动关闭此窗口');
  69. }
  70. }, 300);
  71. } else {
  72. // 没有 opener(不是通过 window.open 打开的)
  73. alert('当前页面不是通过 window.open 打开的,无法使用 postMessage。\n请确认跳转方式是否为 window.open()');
  74. }
  75. });
  76. </script>
  77. </body>
  78. </html>