Mirror Image Iframes

Currently, I have been tasked with constructing a side-by-side "frame" using HTML. The idea is that when the content in the iframe on the left is selected, it will appear in the iframe next to it on the same page.

Here's some additional context: The menu bar on the left side is generated from Tableau, while the visualization itself is displayed on the right-hand side. Despite trying to implement the 'target' attribute in the code snippet provided below, it has not yielded the desired result:

<div id="left">
<iframe src="http://www.weather.gov/" target="myDemoFrame"></iframe>
</div>

<div id="right">
<iframe name="myDemoFrame" src=""></iframe>
</div>

I would greatly appreciate any suggestions or advice on how to achieve this. Thank you!

Answer №1

If you have control over both pages, you can communicate between the iframes using postMessage. In this scenario, the left window would message the parent and the parent would message the right child. Here is a sample setup:

outer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .frame {
            width: 45vw;
            height:95vh;
        }
    </style>
</head>
<body>
<iframe src="left.html" class="frame" id="left"></iframe>
<iframe src="right.html" class="frame" id="right"></iframe>
</body>
<script>
    var l = document.getElementById("left");
    var r = document.getElementById("right");
    window.onmessage = function(e){
      if (l.contentWindow == e.source) {
        r.contentWindow.postMessage(e.data, '*')
      }
    };
</script>
</html>

left.html

<!DOCTYPE html>
<html lang="en">
<body>
<button onclick="clickEvent()">SEND</button>
</body>
<script>
    function clickEvent() {
      window.top.postMessage({"payload": "Hello from the other side"}, '*')
    }
</script>
</html>

right.html

<!DOCTYPE html>
<html lang="en">
<body>
</body>
<script>
  window.onmessage = function(e){
    if (e.source == window.top) {
      if (e.data["payload"]) {
        alert(e.data["payload"]);
      }
    }
  }
</script>
</html>

Edit - Pure navigation answer

outer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .frame {
            width: 45vw;
            height:95vh;
        }
    </style>
</head>
<body>
<iframe src="left.html" class="frame" id="left"></iframe>
<iframe class="frame" id="right"></iframe>
</body>
<script>
    var l = document.getElementById("left");
    var r = document.getElementById("right");
    window.onmessage = function(e){
      if (l.contentWindow == e.source && e.data["payload"]) {
        r.src = e.data["payload"]
      }
    };
</script>
</html>

left.html

<!DOCTYPE html>
<html lang="en">
<body>
<button onclick="navigate('http://www.bing.com')">BING</button>
<button onclick="navigate('http://www.example.com')">EXAMPLE</button>
</body>
<script>
    function navigate(url) {
      window.top.postMessage({"payload": url}, '*')
    }
</script>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A more concise method to verify if something is undefined in JavaScript

Anyone have suggestions for a more concise idiom to use? const x = module || window; // Reference Error fallback Is there a shorter method to verify the presence of module? ...

When using Node.js, res.render may encounter issues, but res.send typically functions properly

Currently, I am in the process of setting up the environment for a node.js app. Unfortunately, I am encountering an issue where the views/ejs files are not being rendered properly. When I use the following code snippet: app.get("/", function(req, res){ ...

What is the best way to toggle a div and dynamically load content into it while it's open?

I am looking to create a toggle effect where a div opens, loads a page within it, and then can be closed again. My goal is to achieve this functionality with multiple links on the page. <div id="main-nav"> <div id="menu-container"&g ...

What is preventing the factory from gaining access to the controller?

I have set up a notification factory and passed it inside the controller. However, when I try to assign the factory to the scope within the controller, I am encountering an error. alertsManager MyApp.factory('alertsManager', function() { ...

Sign in and view SESSION data on the current page without any need to refresh the

My website currently features a login form at the top of each page for users to input their username and password. Once the submit button is clicked, I utilize jQuery AJAX method to send the data to login.php without refreshing the page. Here, the credenti ...

Creating a directive that dynamically expands templates for varying blocks of code based on different ng-repeat values

I have a snippet of code that I want to reuse across various HTML pages. The code in question is as follows: <h5>Brand</h5> <div> <div ng-repeat="brand in store.brands.tops"> <label> <input type="checkbox" ng-t ...

This function does not have the capability to save cookies

I am attempting to create and retrieve a cookie using JavaScript. Despite running the code below in Google Chrome, the cookie does not persist after page reload. Here is my code: <html> <title> Cookies </title> <b ...

The skipping of crucial text in tab focus is adversely affecting the accessibility of the website

My appbar contains an image link, a warning indicating the user's current environment, and details about the logged-in user. While testing accessibility with a screen reader, I noticed that tab focus skips over the user details in favor of a link in ...

Display an empty string when a value is NULL using jQuery's .append() function

To set an HTML value using the .append() function, I need to include AJAX data values. If the data set contains a null value, it will show as 'null' in the UI. I want to remove that 'null' and display it as blank. However, I can't ...

JavaScript: locating web addresses in a text

Need help searching for website URLs (e.g. www.domain.com) within a document and converting them into clickable links? Here's how you can do it: HTML: Hey there, take a look at this link www.wikipedia.org and www.amazon.com! JavaScript: (function( ...

Application frozen after printing <div>

I'm currently working on a project for a client and I have encountered an issue that I would appreciate some guidance on. My task involves printing the contents of a specific div on the website. After finding a solution in a previous post, I implement ...

What is the best way to set conditions for document side script?

I'm struggling to disable the horizontal scroll when the viewport width is 480px or less. The script that controls the scroll behavior on my website looks like this: <script> $(function () { $("#wrapper").wrapInner("< ...

Handling multiple patch requests using React and Redux when onBlur event occurs

Currently, I am using Redux-form for editing guest information. Whenever a field is left, the content of that field gets patched to the guest through a simple patch request and the store is updated accordingly. However, an issue arises when I use Google fo ...

What is the best way to make this CSS arrow see-through?

My goal is to achieve this desired outcome: Currently, I have the following (focusing only on the left element for now): I am struggling to make the left arrow transparent and I can't seem to figure out how. This is the CSS Code I have so far: .ma ...

What is the best way to create a grid item that maximizes the available space in ReactJS Material-UI?

Currently, I am working with the material-ui grid system in a reactjs project. In a column grid container layout, my goal is to display two grid items and have a third item fill up the remaining space between them. Essentially, I want it to look like this: ...

Enhancing the structure and authentication of license plates

I'm having trouble figuring out how to apply Javascript to the text area. Can you advise on whether it's best to use onfocus, onblur, or onclick? Here is the code for the text area: <input type="text" name="c_Kenteken" id="invoerKenteken" cl ...

Ways to increase the values in a textbox

My task involves handling checkboxes that are populated with data from a database. The goal is to have certain checkboxes pre-checked based on the retrieved data, and if any are not checked, their values should be entered into a textbox. For instance, con ...

Re-sequence a contiguous array by mapping and filtering its elements

Within my React code, I have a list component that utilizes array.map to display a list of items. The list items have alternating backgrounds; every other item's background color is determined by whether the id field from the data structure is even o ...

Incorporating Angular module into the mean.io bundle

Need help with adding the angular-ui-grid module to a mean.io package: $ cd packages/custom/mypackage $ npm install angular-ui-grid --save To import the JS, add this line to packages/custom/mypackage/public/index.js: import 'angular-ui-grid'; ...

Creating Unique Identifiers in ExpressJS

I am currently utilizing mongoose to display admin and user information on a dashboard, but I am encountering difficulty rendering the id of a user. Below is the code I am using: function ensureAuthenticated(req, res, next){ if(req.isAuthenticated()){ ...