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

Transforming multi-layered form data into a JSON array structure for seamless integration with application/json

I'm currently developing a Laravel application and facing an issue with the $controller->wantsJson() method in the back-end. This method returns TRUE only if the content type of the request is set to application/json. jQuery.ajax({ type: ...

Is it possible to create a table using a loop in an SQLite query?

Welcome In the company where I work, Excel is currently being used to manage a large database of items for cost estimation purposes. To simplify this process, I am developing an Electron App that will replace Excel with a more user-friendly interface and ...

Disabling padding on TwitterTweetEmbed component in React Twitter Embed

Having an issue with Material UI and React Twitter Embed, unable to remove top and bottom margins. Here's the code snippet causing concern: const styles = theme => ({ listItem: { padding: '0px', }, tweetSize: { margin: 0, ...

Platform designed to simplify integration of high-definition imagery and scalable vector illustrations on websites

In the ever-evolving world of technology, some clients support advanced features like svg while others do not. With the rise of high resolution devices such as iPhone 4+ and iPad 3rd gen., it has become crucial to deliver graphics that can meet these stand ...

Navigating to a different page by clicking on a Table Row in react-table

Whenever I click on a table row, I am unable to navigate to another page. Although I have successfully implemented the getTdProps function to retrieve properties from the table row upon clicking on it, I'm facing difficulty in using 'react-route ...

The replace method for strings in JavaScript does not function properly on mobile devices

I encountered an issue with the string replace method I implemented on my website. Upon checking the page using the web browser on my Android phone, I noticed that it was not functioning as intended. Here's a snippet of the code: /*The function is ...

Adjust the background color of a specific list item when hovering over another list item

My dilemma lies in my inadequate knowledge to solve this issue: I have a dropdown menu embedded within a website. (View screenshot here: [SCREENSHOT]) The desired outcome is for the background color of an icon on the main list to change when I navigate to ...

Is there a way for me to produce a random choice depending on the option selected by the user in the <select> menu?

As a beginner in JavaScript, I am attempting to create a feature where users can select a genre from a dropdown list and receive a random recommendation from that genre displayed on the screen. In essence, my goal is to allow users to get a random suggest ...

How can we monitor the value of $route.params.someKey in Nuxt.js?

When working with Nuxt.js, I am trying to keep track of the value associated with a key called key1 in the $route.params. The values for key1 are determined using a function called someFunction(). Is there a way for me to monitor $route.params.key1 as a ...

Sharing variables between different routes in ExpressJS

Currently, I am utilizing ExpressJs with Node.js and have organized all my routes in a 'routes' folder. When setting up the server, my process involves establishing a DB connection followed by defining the routes as shown below: var routes = re ...

Unravel and extract information from a JavaScript object using PHP

I am working with a variable called "type" in my code. var type = []; Within this variable, I am using the method push to add an object {'type' : 1}. type.push({'type' : 1}); After performing this operation, I end up with the follow ...

Why are there no borders around the rows and columns of my table?

I'm just starting to learn HTML so I appreciate your patience with what may be a basic question for some. After creating a table using , I've noticed that there are no visible lines separating the cells. Is there a way to add lines around the ce ...

Is there a way to prevent users from right clicking on all links with the same class using js/jquery?

Rails 4 + JS + jquery Is there a way to disable right click on links with the same class in Rails? <% @schedule_hash.values.each do |schedule| %> <%= link_to "Cancellation policy", {:controller => 'web', :action => 'get ...

Accessing the phone number input from a form in Rails and verifying its presence in the database. Implementing distinct ajax responses based on the existence of the value

Looking for a way to verify phone numbers? The process involves users entering a phone number, submitting the form, and cross-referencing it with a database of phone number records to determine if it's valid. My goal is to provide different AJAX respo ...

Using CSS styling to dictate how browsers display web content, specifically through setting font size measurements in EM

On different mobile devices and various browsers such as Internet Explorer and Mozilla, the font size in some divs does not appear consistent. Specifically, on a Samsung Galaxy S6. I am using EM units: I know it may not be ideal but I prefer this method a ...

Guide to retrieving data from a URL and storing it in a string variable with JavaScript

Attempting to retrieve the JSON data from a specific URL and store it in a variable. The code snippet below successfully loads the JSON into a div element: $("#siteloader").html('<object data="MYURL">'); However, the goal is to extract t ...

Transforming the appearance of the menu element in Vue using transitions

In my Vue app, I have this SCSS code that I'm using to create a smooth transition effect from the left for a menu when the isVisible property is set to true. However, I am encountering an issue where the transition defined does not apply and the menu ...

"Utilizing images within an iframe: A step-by-step guide

I'm trying to integrate an iframe into a phone image with a transparent background. However, I am unsure how to effectively mask the iframe so that it only appears within the boundaries of the phone image. .phone { display: block; position: r ...

Use jQuery in MVC 5 to remove each cloned div individually starting from the last one, excluding the default div

My default setting is as shown below. https://i.stack.imgur.com/m8Hpr.png The add button functions correctly, but I am having trouble removing cloned divs. I can only remove the last one, instead of each cloned div individually starting from the last, exc ...

Utilize xdebug and NetBeans 8.2 to troubleshoot PHP code submitted through an HTML form

After successfully setting up the XDebug extension to debug PHP code within NetBeans IDE (ver. 8.2), I encountered an issue. While debugging works fine when setting a breakpoint and running the script in debug mode, I am having trouble debugging PHP code t ...