What is the best way to transfer a variable from an iframe to its parent window?

My colorbox parent page includes a TabContainer with various iFrames, and in the case of the second tab, a change is made to the database that requires updating a textbox on the parent window.

Answer №1

Regrettably, interacting beyond the boundaries of an iframe is not possible due to security concerns.

A potential solution is to modify the URL inside the iframe by adding a #anchor value or ?querystring, and then retrieve the updated URL from the parent element.

Answer №2

If you haven't already, give postMessage a try. It's a JavaScript API, but be aware of browser limitations.

The parent page can send messages like this:

var win = document.getElementById("iframe").contentWindow

win.postMessage(
  "value",
  "iframe-domain"
);

Meanwhile, the iframe page can receive and process these messages using the following code:

<script>
function listener(event){
  if ( event.origin !== "[parent-page-domain]" )
    return;

 // Process event.data here
}

if (window.addEventListener){
  addEventListener("message", listener, false)
} else {
  attachEvent("onmessage", listener)
}
</script>

SOURCE - http://javascript.info/tutorial/cross-window-messaging-with-postmessage

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

Ways to set a default image for an <img> tag

I am looking to set a default image to the img tag in case the user has not selected a profile picture for their account. Here is the current output: http://jsfiddle.net/LvsYc/2973/ Check out the default image here: This is the script being used: funct ...

Adding setTimeout within the Axios Scope can enhance the functionality and performance of your

Show an alert in the catch block of Axios. The issue at hand: Error message does not disappear after the specified time when using setTimeout. ...

Recover Checkmark Status from Data

Currently, I am storing form data in json format and encountering an issue when trying to load values from a previously created json file. The plain old JavaScript function provided below successfully loads values into a form from a file, effectively resto ...

jquery autocomplete utilizing an external json data feed

I am facing an issue with my autocomplete function that was initially working with a local json source. I have decided to move it to an external json file, as my current code is lengthy (16k lines). However, I am struggling to make it work with the externa ...

I'm not sure how I can retrieve the pollId from a ReactJS poll

In my React code, I am fetching a poll using an API. However, I am facing an issue while working on the handleChange function for making a POST API request. The information required for the request includes PollId, userId, and answer. I am able to retrieve ...

A method to implement an If Loop on a cube using Threejs in order to generate an interactive on-hover feature that enlarges the cube's size

Having just completed my first tutorial in threejs as a novice, I am now facing a challenge. I am trying to create a hover effect on a basic cube shape where it grows in size when the mouse pointer hovers over it and shrinks back to its original size when ...

Module fails to load in the proper sequence

As a .NET developer who is relatively new to modern client-side web applications, I am currently working on developing an Angular2 charting application using Chart.js. The modules are being loaded with SystemJS. Below is the content of my systemjs.config. ...

What steps can be taken to ensure that a class is able to implement an interface only when it meets the criteria of

Suppose there is an interface named "XMLControl", and I want every implementing class to also implement ISerializable. Is there a way to enforce this using object-oriented principles? An alternative could involve making XMLControl an abstract class, altho ...

Handle a failed internet connection exception during a button click event

Is there a way to detect when the web client is trying to connect without internet on the phone app, as it currently causes an unhandled exception and crashes the application? ...

Unlocking the TypeScript UMD global type definition: A step-by-step guide

I have incorporated three@^0.103.0 into my project, along with its own type definitions. Within my project's src/global.d.ts, I have the following: import * as _THREE from 'three' declare global { const THREE: typeof _THREE } Additio ...

The function that can be used in Ajax for setting

I'm currently utilizing Ajax to automatically submit a form when a key is pressed. After the form is submitted, the page displays the resulting information success: function (response){ $("#search_results<?php echo $HoursID ?>").html(response) ...

Is it possible to obtain the socket.id of a user immediately upon their connection?

Does anyone know how I can send a personalized message to a user when they connect, without broadcasting it to everyone else? I'd like to use their socket ID with the code io.to(theirSocketID).emit('chat message', 'Welcome');, but ...

Troubleshooting an Ajax Error

Encountering an issue when trying to bind two components (checkbox and label) by using the "for" tag attribute for the label and the "id" tag attribute for the checkbox. An ajax debug error is thrown: "Cannot bind a listener for event "click" on element "v ...

The Ubuntu virtual machine hosted on Google Cloud is experiencing difficulties connecting through Node.js using an external IP address

const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const app = express(); app.listen(3000, function(){ console.log('Server is now live on port 3000' ...

Struggling to change the div content with ajax response transmitted through php

I would like to apologize in advance if this question has already been addressed elsewhere. After conducting a search, I came across several relevant posts that have guided me to this point. Below is the form snippet containing 1 input box, a button, and ...

"Can someone guide me on passing a nested JSON array object into the Google Map feature of Angular

Recently, I started working with AngularJS and I've been attempting to pass my JSON data containing latitude and longitude values to the Google Maps API. Here's the structure of my JSON file: { "totalCount":206, "deals":[{ "id":"2", ...

Turn off the Right click option on an .aspx page, but allow it on a Hyper

Can the right click be disabled on a webpage while still allowing it on hyperlinks within the same page? I am aware that I can disable right click, but if there is a way to allow it on hyperlinks, please provide the code. The main reason for disabling rig ...

"Developing a JSON object from a Form: A Step-by-

Currently utilizing the Drag n Drop FormBuilder for form creation. My objective is to generate a JSON representation of the form as shown below: { "action":"hello.html", "method":"get", "enctype":"multipart/form-data", "html":[ { ...

Instructions for activating the click function for each individual looping image

When creating a blog page, I am using PHP to loop the like button images according to the total count of posts (example: Facebook like button). The issue I am facing is that while the PHP loop is working, when I click on the first post image, only the firs ...

Creating a DatePicker in ASP.NET MVC using Visual Basic programming language

Hello there! I am currently involved in a project utilizing Visual Studio 2015, ASP.NET 5, and MVC. I would like to learn more about developing in Visual Basic in order to implement a datepicker feature for entering dates using a calendar within the date ...