


Using postMessage() to implement information transfer method between cross-domain iframe pages_javascript skills
Due to the restrictions of the web origin policy, when the page uses a cross-domain iframe link, the main page and the sub-page cannot interact, which causes a lot of trouble in the transfer of information between pages. After a series of attempts, Finally I found the following method to achieve it:
1. Pass parameter of sub-page url
To put it simply, add all the parameters that need to be passed to the url that has the same origin as the main page, redirect the sub-page to the url, and then the main page obtains these parameters through the src of the iframe
The process is very complicated and this method is not recommended
2. postMessage()
postMessage() is an event-based message transmission API provided by HTML5, which can realize cross-text document, multi-window, and cross-domain messaging.
postMessage(data,origin) method accepts two parameters
1.data: The data to be passed. The html5 specification mentions that this parameter can be any basic type or copyable object of JavaScript. However, not all browsers can do this. Some browsers can only Processing string parameters, so we need to use the JSON.stringify() method to serialize object parameters when passing parameters. Similar effects can be achieved by referencing json2.js in lower versions of IE.
2.origin: String parameter, indicating the source of the target window, protocol + host + port number [+URL]. The URL will be ignored, so it does not need to be written. This parameter is for security reasons. The postMessage() method only The message will be passed to the specified window. Of course, if you like, you can also set the parameter to "*", which can be passed to any window. If you want to specify the same origin as the current window, set it to "/".
Send message (subpage)
function sendMessage(param) { var url; if (param.url) { url = param.url; }; var dataJson = JSON.stringify({ type:1, a: param.c, b: param.c, c: param.c, url: url }); window.parent.postMessage(dataJson, '*'); }
Since some browsers can only handle string parameters, we need to first use JSON.stringfy() to convert the parameters into strings, and then use JSON.parse() on the receiving page to convert them back into objects.
Receive message
For the parameters passed by the sub-page, we can obtain them by listening to the message event of the window:
window.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
switch (data.type) {
case 1:
alert(data.a);break;
}
}, false);
The message event has several important attributes
1.data: As the name suggests, it is the message passed in
2.source: the window object that sends the message
3.origin: the source of the message window (protocol + host + port number)
Cross-domain message delivery can be achieved through the postMessage() method and message event. It should be noted that in the demo, we deliver messages to the parent page through the child page, so we use window.parent to send and window to receive:
window.parent.postMessage(dataJson, '*'); If it is from the homepage to the subpage, it needs to be swapped. Use window to send and window.frames[0] to receive.
The above article uses postMessage() to realize information transfer between cross-domain iframe pages. This is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Inline frames are called iframes in HTML. A label specifies a rectangular area within the content where the browser can display different documents with scroll bars and borders. To embed another document within the current HTML document, use inline frames. A reference to an element can be specified using the HTMLiframe name attribute. In JavaScript, references to elements are also made using the name attribute. An iframe is essentially used to display a web page within the currently displayed web page. The URL of the document containing the iframe is specified using the "src" attribute. Syntax The following is the syntax of HTML <iframesrc="URL"title="d

The data-id in an iframe refers to a custom attribute used in HTML tags to store the identifier of a specific element. By using the data-id attribute, you can add a unique identifier to the iframe element so that it can be manipulated and accessed in JavaScript. The naming of the data-id attribute can be customized according to specific needs, but some naming conventions are usually followed to ensure its uniqueness and readability. The data-id attribute can also be used to identify and manipulate a specific iframe.

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

The loading events of iframe include onload event, onreadystatechange event, onbeforeunload event, onerror event, onabort event, etc. Detailed description: 1. onload event, specifying the JavaScript code to be executed after loading the iframe; 2. onreadystatechange event, specifying the JavaScript code to be executed when the iframe state changes, etc.

The dangers in iframes mainly include: 1. Security vulnerabilities. Malicious web pages can load other web pages through iframes and carry out some attacks; 2. Same-origin policy breakthrough. By loading web pages under other domain names in iframes, the same-origin policy can be breached. strategy to achieve cross-domain communication, which may be maliciously attacked; 3. Code execution issues, web pages loaded in iframes can execute JS code, which may cause some security issues; 4. SEO issues, search engines may not be able to correctly parse and Index content loaded via iframe and more.

iframe in Python is an HTML tag used to embed another web page or document in a web page. In Python, you can use various libraries and frameworks to process and manipulate iframes, the most commonly used of which is the BeautifulSoup library, which can easily extract the content of an iframe from a web page and manipulate and process it. Knowing how to handle and manipulate iframes is extremely useful for both web development and data scraping.

How to monitor the scrolling of an iframe requires specific code examples. When we use the iframe tag to embed other web pages in a web page, sometimes we need to perform some specific operations on the content in the iframe. One of the common needs is to listen for the scroll event of the iframe so that the corresponding code can be executed when the scroll occurs. The following will introduce how to use JavaScript to monitor the scrolling of an iframe, and provide specific code examples for reference. Get the iframe element First, we need

iframe embedded player is a technology that embeds a video player in a web page. The advantages of the embedded player are: 1. Flexibility, by using iframe tags, video media from different sources can be embedded into the same web page; 2. Ease of use, just copy and paste the embed code to play The player can be added to the web page; 3. The appearance and behavior of the player can be controlled by setting parameters; 4. The operation of the player can be controlled by using JavaScript, etc.
