Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!