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

137
Views
communicating between my page and iframe and sending data to each other

I'm trying to communitacting between my page and another iframe that is another page, which I have,

On my iframe page, I inserted this script on the head

if(typeof window !== 'undefined'){
 window.addEventListener('message', event => {
    if (event.origin.startsWith('https://testpage.com/')) { 
        document.querySelector('.home-title').classList.add('test');
        event.source.postMessage('iframe', event.origin);
    } else {
        
        return; 
    } 
 }); 
}

and on my main page where I want to communicate with my iframe, I have this code,

<script>
  window.onload = function () {
  setTimeout(() => {
     const iframe = document.querySelector('iframe');
     const iframeWindow =  iframe.contentWindow;
     iframeWindow.postMessage("Hello there" , 'https://testIfame.com/');
     alert('here alert pops-u');
  }, 3000)
 

 window.addEventListener('message', function(event) {
     if (event.origin == 'https://testpage.com/'){
       alert('asd');
      } 
    }, false);
  }
</script>

The first alert, works, it pops up, but nothing else. Can somebody help me to resolve this. Thanks a lot.

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!