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

129
Vistas
How to add additional window.onmessage in javascript?

I made some javascript tool for internal usage that will be injected to some of our websites. My tool should provide communitcation between our main (host) websita and some iframe, so the tool itself is injected without the iframe tag. I don't know what exactly we will have on this website(s), they might or might not use window.onmessage. In my JS tool I need to use window.onmessage, so if the host website uses it as well, we will have a conflict. Can you, please, advise, how can I add additional onmessage or check and if it exist and wrapp it somehow ?

Here is an example of my code in the JS tool:

window.onmessage = (event) => {
    try{
        separator = "[@-------->]";
        GetReqEventTag = 'PluginGetRequestEvent'
        var content = event.data;
        if((typeof content === 'string' || content instanceof String) && content.startsWith(GetReqEventTag)){


            var requestParams = content.split(separator);
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                    event.source.window.postMessage([GetReqEventTag, requestParams[1], xmlHttp.responseText].join(separator), '*')
                }
            };
            xmlHttp.open("GET", requestParams[2], true);
            xmlHttp.send(null);
        }
    }
    catch (ex) {
        console.error("Unable to send request to Plugin backend: " +ex, ex.stack);
    }
}
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