Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
Having issues using postMessage with iframe

I am creating an application that will be used on other websites through an iframe. I am trying to create a js script that the host site will attach to their html page. I am able to create the iframe dynamically with javascript. But I also need to send information from the host site to the iframe, this data will authenticate the host site. I am able to send this using iframe.contentWindow.postMessage when attached to a button. But when I try to attach it to a onload event listener I get the error Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('IFRAME DOMAIN') does not match the recipient window's origin ('null'). the domains will be different, how can I get this to work at first load?

const buildIframe = (element) => {
const terpliIFrame = document.createElement("iframe");
terpliIFrame.setAttribute("src", "https://flutter-plugin-1b70a.web.app/#/");
terpliIFrame.style.width = "340px";
terpliIFrame.style.height = "600px";
terpliIFrame.style.zIndex = "2147483647"
terpliIFrame.style.overflow = "hidden";
terpliIFrame.style.position = "fixed";
terpliIFrame.style.bottom = "5px";
terpliIFrame.style.right = '10px';
terpliIFrame.setAttribute("allowtransparency", "true")
terpliIFrame.setAttribute("frameBorder", "0")
 // I would like to send the postMessage here before I append it to the page
element.appendChild(terpliIFrame);
return terpliIFrame;

}

I am able to have it work when I attach it to a button click

if (addToCartButton) {
   addToCartButton.addEventListener("click", () => {
      iframe.contentWindow.postMessage(JSON.stringify({url: originURL, retailToken: retail}) , "DOMAIN")
})

}

When I use this function:

iframe.onload = () => {
    console.log("hello")
    iframe.contentWindow.postMessage(JSON.stringify({url: originURL, retailToken: retail}) , "Domain")

}

I do not get an error, it console logs hello, bit does not properly send it to the iframe

Can anyone help?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda