index.html 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Iframe Example</title>
  5. <style>
  6. #myIframe {
  7. width: 600px;
  8. height: 400px;
  9. border: 1px solid black;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>Parent Page</h1>
  15. <iframe id="myIframe" src="iframe-content.html"></iframe>
  16. <script>
  17. let _log = console.log;
  18. // console.error = () => {}
  19. console.log = (...args) => {
  20. _log.apply(this, args)
  21. }
  22. // JavaScript in the parent page (can interact with the iframe if same origin)
  23. const iframe = document.getElementById('myIframe');
  24. // Example: Sending a message to the iframe (if same origin)
  25. // iframe.contentWindow.postMessage('Hello from parent!', '*'); // '*' means any origin (use with caution)
  26. // Example: Receiving a message from the iframe (if same origin)
  27. window.addEventListener('message', (event) => {
  28. // if (event.origin === 'http://your-iframe-domain.com') { // Check origin for security
  29. console.log('Message from iframe:', event.data);
  30. // }
  31. });
  32. </script>
  33. </body>
  34. </html>