Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:01:54+00:00 2026-06-15T21:01:54+00:00

I have been stuck on this for hours. I have a.html on http://example.com that

  • 0

I have been stuck on this for hours.

I have a.html on http://example.com that contains an iframe with src to
b.html on http://subdomain.example.com. a.html has some JS code
to postMessage to the iframe.

The code to postMessage is simple:

iframe_window.postMessage('message', iframe_element.src)

But this way, Chrome throws an error:

Unable to post message to http://subdomain.example.com. Recipient has origin null.

I have also tried:

iframe_window.postMessage('message', 'http://subdomain.example.com')

But NO LUCK!

This is the ONLY WAY it works:

iframe_window.postMessage('message', '*')

But I have heard ‘*’ is not good to use.

No problems in Firefox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:01:55+00:00Added an answer on June 15, 2026 at 9:01 pm

    It looks like this might be an issue with the child iframe not being loaded at the time the signal is sent, thus iframe.src doesn’t have the proper value.

    I did some testing and got the same error as you, but when I wrapped the postMessage call in a setTimeout and waited 100ms then there was no error, which tells me that this is an initialisation race condition.

    Here’s how I implemented a cleaner solution without the setTimeout:

    Parent:

    window.addEventListener("DOMContentLoaded", function() {
    
        var iframe = document.querySelector("iframe")
          , _window = iframe.contentWindow
    
        window.addEventListener("message", function(e) {
    
            // wait for child to signal that it's loaded.
            if ( e.data === "loaded" && e.origin === iframe.src.split("/").splice(0, 3).join("/")) {
    
                // send the child a message.
                _window.postMessage("Test", iframe.src)
            }
        })
    
    }, false)
    

    Child:

    window.addEventListener("DOMContentLoaded", function() {
    
        // signal the parent that we're loaded.
        window.parent.postMessage("loaded", "*")
    
        // listen for messages from the parent.
        window.addEventListener("message", function(e) {
    
            var message = document.createElement("h1")
    
            message.innerHTML = e.data
    
            document.body.appendChild(message)
    
        }, false)
    
    }, false)
    

    This is a simple solution in which the child will signal to anyone that it’s loaded (using “*”, which is okay, because nothing sensitive is being sent.) The parent listens for a loaded event and checks that it’s the child that it’s interested in that’s emitting it.

    The parent then sends a message to the child, which is ready to receive it. When the child gets the message it puts the data in an <h1> and appends that to the <body>.

    I tested this in Chrome with actual subdomains and this solution worked for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this for few hours and got stuck, so how
Its been hours now that I've been stuck in this problem, does anyone know
Im stuck, been trying to figure this out for 2 hours now. I have
I have been stuck on this for hours..I downloaded the certificate from my developer
This seems to be a pretty trivial problem that I have been stuck on
I have been stuck with this prolem for the past 5 hours or so..
So I have been stuck for a couple of hours trying to get this
I have been playing with this for a few hours and I'm stuck. I'm
I have been stuck on this problem for many hours, but I have a
I have been stuck on this for a while now and I cannot seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.