“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
iframe
What is?
The IFrame label specification is an inline framework. An inline frame is used to embed another document within the current HTML document. The use effect is as follows:
iframe
The advantages and disadvantages of
Advantages:
iframe
The embedded web page can be displayed intact;- If there are multiple page references
iframe
, then only need to modifyiframe
The content can be implemented to call the changes of each page, convenient and quick; - A web page can be written as a page if the header and version are the same for the sake of uniformity
iframe
Nesting, which can increase code reuse;
Disadvantages:
iframe
Block the Onload event on the main page.iframe
The connection pool is shared with the main page, and browsers have restrictions on connections to the same domain, so parallel page loading can be affected.- Code complex, can not be indexed by some search engines (some search engines on the framework of the page can not handle correctly, will affect the ranking of search results);
- Multi-frame pages will increase the HTTP requests of the server, affect the parallel loading of pages, and increase the load of the server.
Although it has many disadvantages, it is still very common in our daily use.
iframe
interaction
With the domain name underiframe
Parent-child page communication
1. The parent page calls the child page method
//html
<iframe id="zi" name="zi" src="zi.html" frameborder="0"></iframe>
// js
// Get the child page method
document.getElementById('zi').contentWindow.ziFn();
// Parent page method
function fuFn() {
console.log("I'm the parent page method");
}
Copy the code
2. The child page calls the parent page method
// js
function ziFn() {
console.log('I'm a child page method');
}
function func() {
// Get the parent object, window can be omitted
parent.window.fuFn();
}
Copy the code
Under the cross domainiframe
Parent-child page communication
Children pass values to their parents
- The child
iframe
code
window.parent.postMessage('msg'.The '*');
Copy the code
- The father
iframe
code
window.addEventListener('message'.function(event){
if(event.data=='msg') {console.log(event); }})Copy the code
The parent passes value to the child
- The child
iframe
code
window.addEventListener('message'.function() {
if(event.data=='msg') {console.log(event); }});Copy the code
- The father
iframe
code
fuFrame.contentWindow.postMessage('msg'.The '*');
Copy the code
Matters needing attention:
- The parent window sends a message to the child window
iframe
thecontentWindow
Property as the call body; - The message sent by the child window to the parent window needs to be used
window.parent
The multilayeriframe
usewindow.frameElement
Get the top-level window can be usedwindow.top
; - To ensure that the
iframe
Load complete, as iniframe
An error is reported if a method or variable is called before the load is complete. You can useonload
Event or withdocument.readyState=="complete"
To determine whether the load is complete;