Front-end cross-domain page data transmission method and system

By using iframe + postMessage in front-end pages, the problems of insecurity and limited capacity in cross-domain data transmission are solved, secure data interaction and efficient data transmission between different domains are realized, and the efficiency of front-end page development is improved.

CN118963900APending Publication Date: 2024-11-15TONGFANG YOUYUN (BEIJING) TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411136477.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-19
Publication Date
2024-11-15

AI Technical Summary

Technical Problem

The existing technology has problems such as insecurity, limited capacity and low reliability in cross-domain data delivery, especially location.hash + iframe, window.name + iframe and iframe + domain solutions, which cannot effectively solve the problem between different domains. Secure communication and big data transmission requirements.

Method used

Using iframe + postMessage, we use the postMessage API to conduct secure data interaction between different domains, use iframe and postMessage to achieve cross-domain messaging, and set a domain name whitelist to ensure data security and reliability.

Benefits of technology

It realizes secure data interaction between different source pages, supports large data transmission, improves the efficiency of front-end page development, and ensures data security and reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118963900A_ABST
    Figure CN118963900A_ABST
Patent Text Reader

Abstract

The invention provides a front-end cross-domain page data transmission method and system. The method comprises the steps that the src attribute of a parent page iframe is set as the URL of a sub-page needing communication; after the child page is loaded, a message is sent to the parent page by using a postMessage method; the parent page monitors a message event, and receives and processes the message sent by the child page in an event processing function; when the sub-page needs to obtain a parent page message, the parent page sends the message to the sub-page by using a post Message method; and the sub-page monitors the message event, and receives and processes the message sent by the parent page in the event processing function. According to the invention, data can be prevented from being leaked to an unauthorized domain; data transmission is simple and easy, and other tasks can be processed at the same time; communication can be carried out among different documents or different windows, pages from different sources are allowed to carry out safe data interaction, and the front-end page development efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of front-end page development; in particular, to a front-end cross-domain page data transmission method and system. Background Art

[0002] In the front-end field, cross-domain means that the browser allows the server to send cross-domain requests, thereby overcoming the limitation that ajax can only be used from the same source. The so-called same source means that the "protocol + domain name + port" are the same. Even if two different domain names point to the same IP address, they are not from the same source.

[0003] The same-origin policy is a browser security policy introduced by Netscape in 1995. It is the core and most basic security function of the browser. The same-origin policy is a convention. The original intention of the same-origin policy is to ensure the security of the browser. If the same-origin policy is missing, the browser is very vulnerable to attacks such as XSS and CSFR.

[0004] The same-origin policy restricts the following behaviors: (1) Cookies, LocalStorage and IndexDB cannot be read; (2) DOM and JS objects cannot be obtained; (3) The Ajax request cannot be sent.

[0005] Cross-domain access is to bypass the restrictions of the same-origin policy through certain means to achieve the effect of communication between pages under different domain names. At present, there are mainly the following cross-domain solutions for the need to access data under other domain names: 1. JSONP method. The principle of this method is: <script> 标签不存在跨域请求的限制,利用<script> 标签可以请求到其他域名的页面上的数据。然而,JSONP方法存在以下缺点:(1)只能处理 get 请求;(2)通过 URL 携带参数容易被挟持,不安全;2、基于 iframe 的 document.domain方法,该方法的原理是:两个页面都通过 js强制设置 document.domain 为基础主域,从而实现跨域。然而,基于 iframe 的document.domain方法存在的缺点是:只能实现同一个主域、不同子域之间的操作3、location.hash + iframe方法,该方法的原理是:a 页面欲与 b页面跨域相互通信,通过中间页 c 来实现,a、b、c三个页面之间,对于不同域之间利用 iframe 的location.hash 传值,对于相同域之间直接 js 访问来通信。然而,location.hash +iframe方法存在的缺点是:location.hash 会直接暴露在 URL 里,并且在一些浏览器里会产生历史记录,数据安全性不高,也影响到用户体验;另外由于URL大小的限制,支持传递的数据量不大。

[0006] 4、window.name + iframe方法,该方法的原理是:name 值在不同的页面(甚至不同的域名)加载后依然存在,并且可以支持非常长的 name 值(2MB),通过iframe的src属性由外域转向本地域,跨域数据即由iframe的 window.name 从外域传递到了本地域。然而,window.name + iframe方法存在的缺点是:window.name 容量有限;window.name 的方式只能用于支持跨域的 HTTP 协议对于其他协议(如 FTP)不支持;在某些浏览器中,如果iframe 被重定向到一个不同的域,那么 window.name 的值会丢失,可靠性低。发明内容

[0007] 鉴于此,本发明的目的在于提出一种前端跨域页面传递数据方法及系统,采用iframe + postMessage 的方式,基于iframe和 postMessage作为适用于不同域之间安全通信的 API的特性,解决现有技术的location.hash + iframe,window.name + iframe,iframe + domain 等方案存在的不安全以及容量有限、可靠性低等局限性问题,实现来自不同源的页面(跨域)进行安全的数据交互,从而提高前端页面开发效率。

[0008] 本发明提供前端跨域页面传递数据方法,包括以下步骤:S1、设置父页面(A域)的iframe 的 src 属性为需要通信的子页面(B 域)的URL地址;S2、子页面(B域)加载完成后,在所述子页面(B域)使用 postMessage 方法发送消息给所述父页面(A域);postMessage 是 HTML5 XMLHttpRequest Level2 中的 API,且是为数不多可以跨域操作的 window 属性之一,postMessage可用于解决以下方面的问题(1)页面和其打开的新窗口的数据传递;(2)多窗口之间的消息传递;(3)页面与嵌套的 iframe 消息传递;(4)上面 3 个场景的跨域数据传递。

[0009] 结合postMessage 的优势,使用 iframe 和 postMessage 的方式可以解决现有方案location.hash + iframe,window.name + iframe, iframe + domain 等方案存在的不安全以及容量有限等局限性。

[0010] S3、由父页面(A域)监听 message 事件,在事件处理函数中接收并处理所述子页面(B域)发送过来的消息;S4、当子页面(B域)需要获取父页面(A域)的消息时,在所述父页面(A域)使用postMessage方法发送消息给所述子页面(B域);S5、由所述子页面(B域)监听 message 事件,在事件处理函数中接收并处理所述父页面(A域)发送过来的消息。

[0011] 进一步地,所述S2步骤的在所述子页面(B域)使用 postMessage 方法发送消息给所述父页面(A域)包括:在要发送消息的子页面(B域)的窗口或标签页中,使用 PostMessage 方法向父页面(A域)的目标窗口发送消息,父页面(A域)接收到两个参数:要发送的消息对象、目标窗口的标识符,其中,所述目标窗口的标识符是可选的。

[0012] 进一步地,所述S3步骤的由父页面(A域)监听 message 事件包括:在父页面(A域)的目标窗口中,使用window.addEventListener 方法注册 message 事件,在回调函数中定义对接收到数据的处理方式;当父页面(A域)有消息传来时,调用之前定义好的所述回调函数,获取到发送的消息内容。

[0013] 进一步地,所述S4步骤的在所述父页面(A域)使用 postMessage 方法发送消息给所述子页面(B域)包括:在要发送消息的父页面(A域)的窗口或标签页中,使用 PostMessage 方法向子页面(B域)的目标窗口发送消息,子页面(B域)接收到两个参数:要发送的消息对象、目标窗口的标识符,其中,所述目标窗口的标识符是可选的。

[0014] 进一步地,所述S5步骤的由所述子页面(B域)监听 message 事件包括:在子页面(B域)的目标窗口中,使用window.addEventListener 方法注册 message 事件,在回调函数中定义对接收到数据的处理方式;当子页面(B域)有消息传来时,调用之前定义好的所述回调函数,获取到发送的消息内容。

[0015] 进一步地,所述postMessage 方法包括设置消息能够发送到的域的域名白名单,指定目标 origin 域的参数,所述目标 origin 域表示消息能够发送到哪些域,保证只有指定的域能够接收到消息。

[0016] 本发明还提供前端跨域页面传递数据系统,执行如上述所述的前端跨域页面传递数据方法,包括:设置iframe属性模块:用于设置父页面的iframe 的 src 属性为需要通信的子页面的URL地址;子页面发送消息给父页面模块:用于子页面加载完成后,在所述子页面使用postMessage 方法发送消息给所述父页面;父页面监听事件模块:用于由父页面监听 message 事件,在事件处理函数中接收并处理所述子页面发送过来的消息;父页面发送消息给子页面模块:用于当子页面需要获取父页面的消息时,在所述父页面使用postMessage方法发送消息给所述子页面;子页面监听事件模块:用于由所述子页面监听 message 事件,在事件处理函数中接收并处理所述父页面发送过来的消息。

[0017] 本发明还提供一种计算机可读存储介质,其上存储有计算机程序,所述程序被处理器执行时实现如上述所述的前端跨域页面传递数据方法的步骤。

[0018] 本发明还提供一种计算机设备,所述计算机设备包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现如上述所述的前端跨域页面传递数据方法的步骤。

[0019] 与现有技术相比,本发明的有益效果在于:本发明提供的前端跨域页面传递数据方法及系统采用iframe+ postMessage 的方式,使用postMessage能够将消息发送到特定的目标origin域,可防止数据泄漏到未经授权的域;使用 postMessage 传输数据只需调用 postMessage方法并提供所需的数据以及目标origin域即可,简单易行;由于postMessage是异步消息发送函数,不会阻塞主线程,因此可同时处理其他任务;使用postMessage 可实现在不同的文档(iframe)或者不同的窗口(如窗口,标签页)之间进行跨文档通信,允许来自不同源的页面(跨域)进行安全的数据交互,并且支持传递的数据量大,可靠性强,有效提高了前端页面开发效率。附图说明

[0020] 通过阅读下文优选实施方式的详细描述,各种其他的优点和益处对于本领域普通技术人员将变得清楚明了。附图仅用于示出优选实施方式的目的,而并不认为是对本发明的限制。

[0021] 在附图中:图1为本发明实施例的前端跨域页面传递数据的流程架构示意图;图2为本发明前端跨域页面传递数据方法的流程图;图3为本发明实施例计算机设备的构成示意图。具体实施方式

[0022] 这里将详细地对示例性实施例进行说明,其示例表示在附图中。下面的描述涉及附图时,除非另有表示,不同附图中的相同数字表示相同或相似的要素。以下示例性实施例中所描述的实施方式并不代表与本公开相一致的所有实施方式。相反,它们仅是与如所附权利要求书中所详述的、本公开的一些方面相一致的装置和产品的例子。

[0023] 在本公开使用的术语是仅仅出于描述特定实施例的目的,而非旨在限制本公开。在本公开和所附权利要求书中所使用的单数形式的"一种”、"所述”和"该”也旨在包括多数形式,除非上下文清楚地表示其他含义。还应当理解,本文中使用的术语"和 / 或”是指并包含一个或多个相关联的列出项目的任何或所有可能组合。

[0024] 应当理解,尽管在本公开可能采用术语第一、第二、第三等来描述各种信息,但这些信息不应限于这些术语。这些术语仅用来将同一类型的信息彼此区分开。例如,在不脱离本公开范围的情况下,第一信息也可以被称为第二信息,类似地,第二信息也可以被称为第一信息。取决于语境,如在此所使用的词语"如果”可以被解释成为"在……时”或"当……时”或"响应于确定”。

[0025] 下面对本发明实施例作进一步详细说明。

[0026] 本发明实施例提供前端跨域页面传递数据方法,参见图2所示,包括以下步骤:S1、设置父页面(A域)的iframe 的 src 属性为需要通信的子页面(B 域)的URL地址;S2、子页面(B域)加载完成后,在所述子页面(B域)使用 postMessage 方法发送消息给所述父页面(A域);在使用postMessage方法发送消息的过程中,具体地,postMessage函数将消息放入消息队列中后,会立刻返回,消息循环中的 GetMessage 处理到用户发送的消息之后,便按照普通消息的处理方法进行处理。

[0027] 在所述子页面(B域)使用 postMessage 方法发送消息给所述父页面(A域)包括:在要发送消息的子页面(B域)的窗口或标签页中,使用 PostMessage 方法向父页面(A域)的目标窗口发送消息,父页面(A域)接收到两个参数:要发送的消息对象、目标窗口的标识符,其中,所述目标窗口的标识符是可选的。

[0028] S3、由父页面(A域)监听 message 事件,在事件处理函数中接收并处理所述子页面(B域)发送过来的消息;由父页面(A域)监听 message 事件包括:在父页面(A域)的目标窗口中,使用window.addEventListener 方法注册 message 事件,在回调函数中定义对接收到数据的处理方式;当父页面(A域)有消息传来时,调用之前定义好的所述回调函数,获取到发送的消息内容。

[0029] S4、当子页面(B域)需要获取父页面(A域)的消息时,在所述父页面(A域)使用postMessage方法发送消息给所述子页面(B域);在所述父页面(A域)使用 postMessage 方法发送消息给所述子页面(B域)包括:在要发送消息的父页面(A域)的窗口或标签页中,使用 PostMessage 方法向子页面(B域)的目标窗口发送消息,子页面(B域)接收到两个参数:要发送的消息对象、目标窗口的标识符,其中,所述目标窗口的标识符是可选的。

[0030] S5、由所述子页面(B域)监听 message 事件,在事件处理函数中接收并处理所述父页面(A域)发送过来的消息。

[0031] 由所述子页面(B域)监听 message 事件包括:在子页面(B域)的目标窗口中,使用window.addEventListener 方法注册 message 事件,在回调函数中定义对接收到数据的处理方式;当子页面(B域)有消息传来时,调用之前定义好的所述回调函数,获取到发送的消息内容。

[0032] 所述postMessage 方法包括设置消息能够发送到的域的域名白名单,指定目标origin 域的参数,所述目标 origin 域表示消息能够发送到哪些域,保证只有指定的域能够接收到消息。

[0033] 图1示出了本实施例的前端跨域页面传递数据的流程架构。

[0034] 本发明还提供前端跨域页面传递数据系统,执行如上述所述的前端跨域页面传递数据方法,包括:设置iframe属性模块:用于设置父页面的iframe 的 src 属性为需要通信的子页面的URL地址;子页面发送消息给父页面模块:用于子页面加载完成后,在所述子页面使用postMessage 方法发送消息给所述父页面;父页面监听事件模块:用于由父页面监听 message 事件,在事件处理函数中接收并处理所述子页面发送过来的消息;父页面发送消息给子页面模块:用于当子页面需要获取父页面的消息时,在所述父页面使用postMessage方法发送消息给所述子页面;子页面监听事件模块:用于由所述子页面监听 message 事件,在事件处理函数中接收并处理所述父页面发送过来的消息。

[0035] 本发明实施例还提供一种计算机设备,图3是本发明实施例提供的一种计算机设备的结构示意图;参见附图图3所示,该计算机设备包括:输入装置23、输出装置24、存储器22和处理器21;所述存储器22,用于存储一个或多个程序;当所述一个或多个程序被所述一个或多个处理器21执行,使得所述一个或多个处理器21实现如上述实施例提供的前端跨域页面传递数据方法;其中输入装置23、输出装置24、存储器22和处理器21可以通过总线或者其他方式连接,图3中以通过总线连接为例。

[0036] 存储器22作为一种计算设备可读写存储介质,可用于存储软件程序、计算机可执行程序,如本发明实施例所述的前端跨域页面传递数据方法对应的程序指令;存储器22可主要包括存储程序区和存储数据区,其中,存储程序区可存储操作系统、至少一个功能所需的应用程序;存储数据区可存储根据设备的使用所创建的数据等;此外,存储器22可以包括高速随机存取存储器,还可以包括非易失性存储器,例如至少一个磁盘存储器件、闪存器件、或其他非易失性固态存储器件;在一些实例中,存储器22可进一步包括相对于处理器21远程设置的存储器,这些远程存储器可以通过网络连接至设备。上述网络的实例包括但不限于互联网、企业内部网、局域网、移动通信网及其组合。

[0037] 输入装置23可用于接收输入的数字或字符信息,以及产生与设备的用户设置以及功能控制有关的键信号输入;输出装置24可包括显示屏等显示设备。

[0038] 处理器21通过运行存储在存储器22中的软件程序、指令以及模块,从而执行设备的各种功能应用以及数据处理,即实现上述的前端跨域页面传递数据方法。

[0039] 上述提供的计算机设备可用于执行上述实施例提供的前端跨域页面传递数据方法,具备相应的功能和有益效果。

[0040] 本发明实施例还提供一种包含计算机可执行指令的存储介质,所述计算机可执行指令在由计算机处理器执行时用于执行如上述实施例提供的前端跨域页面传递数据方法,存储介质是任何的各种类型的存储器设备或存储设备,存储介质包括:安装介质,例如CD-ROM、软盘或磁带装置;计算机系统存储器或随机存取存储器,诸如DRAM、DDR RAM、SRAM、EDORAM,兰巴斯(Rambus)RAM等;非易失性存储器,诸如闪存、磁介质(例如硬盘或光存储);寄存器或其它相似类型的存储器元件等;存储介质可以还包括其它类型的存储器或其组合;另外,存储介质可以位于程序在其中被执行的第一计算机系统中,或者可以位于不同的第二计算机系统中,第二计算机系统通过网络(诸如因特网)连接到第一计算机系统;第二计算机系统可以提供程序指令给第一计算机用于执行。存储介质包括可以驻留在不同位置中(例如在通过网络连接的不同计算机系统中)的两个或更多存储介质。存储介质可以存储可由一个或多个处理器执行的程序指令(例如具体实现为计算机程序)。

[0041] 当然,本发明实施例所提供的一种包含计算机可执行指令的存储介质,其计算机可执行指令不限于如上实施例所述的前端跨域页面传递数据方法,还可以执行本发明任意实施例所提供的前端跨域页面传递数据方法中的相关操作。至此,已经结合附图所示的优选实施方式描述了本发明的技术方案,但是,本领域技术人员容易理解的是,本发明的保护范围显然不局限于这些具体实施方式。在不偏离本发明的原理的前提下,本领域技术人员可以对相关技术特征做出等同的更改或替换,这些更改或替换之后的技术方案都将落入本发明的保护范围之内。

[0042] 以上所述仅为本发明的优选实施例,并不用于限制本发明;对于本领域的技术人员来说,本发明可以有各种更改和变化。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。< / script>

Claims

1. The front-end cross-domain page data transmission method is characterized by: The following steps are involved: S1. Set the src attribute of the iframe of the parent page to the URL address of the child page that needs to communicate; S2. After the sub-page is loaded, the sub-page uses the postMessage method to send a message to the parent page; S3, the parent page monitors the message event, and receives and processes the message sent by the child page in the event processing function; S4. When the child page needs to obtain a message from the parent page, the parent page uses the postMessage method to send a message to the child page; S5. The child page monitors the message event, and receives and processes the message sent by the parent page in the event processing function.

2. The front-end cross-domain page data transmission method according to claim 1, characterized in that: The step S2 of sending a message to the parent page using the postMessage method on the child page includes: In the window or tab page of the child page to send a message, use the PostMessage method to send a message to the target window of the parent page. The parent page receives two parameters: the message object to be sent and the identifier of the target window, where the identifier of the target window is optional.

3. The front-end cross-domain page data transmission method according to claim 1, characterized in that: The parent page monitors the message event in step S3, including: registering the message event in the target window of the parent page using the window.addEventListener method, and defining the processing method for the received data in the callback function; when a message is received from the parent page, calling the previously defined callback function to obtain the content of the sent message.

4. The front-end cross-domain page data transmission method according to claim 1, characterized in that: The step S4 of sending a message to the child page using the postMessage method on the parent page includes: In the window or tab page of the parent page to send a message, use the PostMessage method to send a message to the target window of the child page. The child page receives two parameters: the message object to be sent and the identifier of the target window, where the identifier of the target window is optional.

5. The front-end cross-domain page data transmission method according to claim 1, characterized in that: The sub-page monitoring the message event in step S5 includes: registering the message event in the target window of the sub-page using the window.addEventListener method, and defining the processing method for the received data in the callback function; when a message is received from the sub-page, calling the previously defined callback function to obtain the content of the sent message.

6. The front-end cross-domain page data transmission method according to claim 1, characterized in that: The postMessage method includes setting a domain name whitelist of domains to which the message can be sent, and specifying parameters of a target origin domain, wherein the target origin domain indicates which domains the message can be sent to, and ensures that only the specified domain can receive the message.

7. A front-end cross-domain page data transmission system, executing a front-end cross-domain page data transmission method as claimed in any one of claims 1 to 6, characterized in that: include: Set iframe attribute module: used to set the src attribute of the iframe of the parent page to the URL address of the child page that needs to communicate; The module for sending messages from a sub-page to a parent page is used to send messages to the parent page using the postMessage method after the sub-page is loaded. Parent page event monitoring module: used for the parent page to monitor message events, and receive and process the messages sent by the child page in the event processing function; The module for sending messages from the parent page to the child page is used to send messages to the child page using the postMessage method on the parent page when the child page needs to obtain messages from the parent page; Sub-page event monitoring module: used for the sub-page to monitor message events, and receive and process the message sent by the parent page in the event processing function.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by the processor, the steps of the front-end cross-domain page data transmission method described in any one of claims 1-6 are implemented.

9. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the front-end cross-domain page data transmission method as described in any one of claims 1-6 are implemented.

Citation Information

Cited By

  • Cross-window data transmission method and device, equipment, storage medium and program product

    CN121901528A