An interactive demonstration of native DOM methods available for same-origin iframe communication and manipulation, including History API navigation.
This project demonstrates how parent windows and iframes can interact with each other when they share the same origin. It showcases various native browser APIs and DOM methods that allow bidirectional communication and manipulation between frames, plus iframe navigation using the History API.
- Access iframe's
contentWindowandcontentDocument - Read and modify iframe DOM elements
- Change iframe styles dynamically
- Call functions defined in the iframe
- Query iframe DOM structure
- Add event listeners to iframe elements
- Trigger custom events in the iframe
- Access parent window via
window.parent - Access top-level window via
window.top - Get the iframe element using
window.frameElement - Modify parent DOM elements
- Update parent input values
- Call functions defined in the parent
- Query parent DOM structure
- Listen to parent events
- Trigger custom events in the parent
- Navigate between multiple iframe pages
- Parent-controlled iframe navigation
- History API methods:
pushState(),replaceState(),back(),forward() - Iframe history management from parent
popstateevent handling- History state preservation across navigation
- Window relationship checking
- Origin comparison
- Event propagation across frames
- Cross-frame function calls
- Shared data access between frames
npm install🌐 View Live Demo on GitHub Pages
The demo is automatically deployed to GitHub Pages via GitHub Actions whenever changes are pushed to the main branch.
For local development, you can use the included Express server:
npm install
npm startOr directly with Node:
node server.jsThe server will start on http://localhost:3000
- Visit the live demo
- Use the interactive buttons to test different iframe DOM methods
- Observe the outputs to see the results of each operation
- Try buttons in both the parent page and inside the iframe content
- Install dependencies:
npm install - Start the server:
npm start - Open your browser and navigate to
http://localhost:3000 - Experiment with the iframe DOM methods interactively
iframe.contentWindow- Access the iframe's window objectiframe.contentDocument- Access the iframe's document object- Direct DOM manipulation across frames
- Cross-frame function invocation
window.parent- Access the parent windowwindow.top- Access the top-level windowwindow.frameElement- Get reference to the iframe element itself- Parent DOM manipulation from inside the iframe
iframe.src = url- Navigate iframe from parentiframe.contentWindow.history.back()- Navigate iframe history from parentiframe.contentWindow.history.forward()- Forward in iframe history from parentwindow.history.pushState()- Add history entry with statewindow.history.replaceState()- Modify current history entrywindow.history.back()/forward()- Navigate through historypopstateevent - Listen for history navigation
addEventListener()- Listen to events across framesdispatchEvent()- Trigger custom events across framesquerySelector()/getElementById()- Query DOM across frames- Direct property access and function calls
All these methods work because the parent and iframe share the same origin (protocol, domain, and port). Cross-origin iframes would be restricted by the browser's security model and would require postMessage() API for communication.
/iframe-hacking/
├── server.js # Express server
├── index.html # Parent page with iframe and controls
├── iframe.html # Content loaded in the iframe
├── package.json # Node.js dependencies
└── README.md # This file
Works in all modern browsers that support:
- ES6+ JavaScript
- Iframe elements
- DOM manipulation APIs
- Custom events
This demo is for educational purposes and demonstrates same-origin iframe interactions. In production:
- Always validate and sanitize data passed between frames
- Be cautious with cross-frame DOM manipulation
- Consider using
postMessage()for more structured communication - Implement proper Content Security Policy (CSP) headers
- Be aware of clickjacking vulnerabilities with iframes
This project is automatically deployed to GitHub Pages using GitHub Actions. The workflow:
- Triggers on every push to the
mainbranch - Deploys all static files (HTML, CSS, JS) to GitHub Pages
- Makes the demo accessible at: https://bobbravo2.github.io/iframe-hacking/
Note: The Express server (server.js) is only needed for local development. GitHub Pages serves the HTML files directly as static content.
You can also trigger a deployment manually:
- Go to the repository on GitHub
- Click on "Actions" tab
- Select "Deploy to GitHub Pages" workflow
- Click "Run workflow"
MIT