Personal website www.kuangyx.cn

Steps:

  1. Parent page opens child page
  2. The child page sends a message to the parent page notifying the page load is complete
  3. The parent page sends data to the child page after receiving the child page

Code example: Parent page

Const targetWindow = window.open(" subpage address ", "_black"); Window. addEventListener("message", e => {if (e.orein == "subpage address ") {console.log(e.ata," data sent from subpage "); TargetWindow. PostMessage (" Data to child page ", "*"); }});Copy the code

Child pages

Window.addeventlistener ('message',e => {console.log(e.data,' data sent from parent page '); }) window. AddEventListener (' load '() = > {window. Opener. PostMessage (' child pages loaded'); })Copy the code