123456789101112131415161718192021222324252627282930313233343536373839 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>Iframe Content</title>
- </head>
- <body>
- <h2>Iframe Content</h2>
- <p>This is the content inside the iframe.</p>
- <button onclick="sendMessageToParent()">Send Message to Parent</button>
- <script>
- function sendMessageToParent() {
- // Send a message to the parent window (if same origin)
- window.parent.postMessage('Hello from iframe!', '*'); // '*' means any origin (use with caution)
- }
- // JavaScript within the iframe
- console.log("Iframe script running");
- // Example: Using debugger statement
- // debugger; // Uncomment to pause execution in DevTools
- // Example: Accessing elements within the iframe (if same origin)
- const iframeContent = document.querySelector('p');
- if (iframeContent) {
- iframeContent.style.color = "blue";
- }
- // Example: Receiving a message from the parent (if same origin)
- window.addEventListener('message', (event) => {
- console.log('Message from parent:', event.data);
- });
- </script>
- </body>
- </html>
|