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 5930635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:34:54+00:00 2026-05-22T14:34:54+00:00

I use a few lines of javascript to create an iframe element, and then

  • 0

I use a few lines of javascript to create an iframe element, and then I’d like to send it a message, like this:

function loadiframe (callback) {
    var body = document.getElementsByTagName('body')[0];
    var iframe = document.createElement('iframe');
    iframe.id = 'iframe';
    iframe.seamless = 'seamless';
    iframe.src = 'http://localhost:3000/iframe.html';
    body.appendChild(iframe);
    callback();
}

loadiframe(function() {
    cpframe = document.getElementById('iframe').contentWindow;
    cpframe.postMessage('please get this message','http://localhost:3000');
    console.log('posted');
})

And then, inside http://localhost:3000/iframe.html (the source of the iframe) is something like this:

<!DOCTYPE html>
<html>
<head>
<title>does not work</title>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
  if (event.origin == "http://localhost:3000") {
    document.write(JSON.stringify(event));
    document.write(typeof event);
  }
}
</script>
</head>
<body>
</body>
</html>

But nothing happens… I even tried to not use the safety check for origin, but even like that nothing happens… Like it never got the message…

Am I in some kind of async problem ? I tried to make sure the iframe was loaded before the postMessage went away…

EDIT1: Also, no errors show on the console…

EDIT2: I tried it in Google Chrome 11 and Firefox 4

Thank you in advance.

  • 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-05-22T14:34:54+00:00Added an answer on May 22, 2026 at 2:34 pm

    There are two possible problems:

    1. You’re calling callback before the child frame has loaded – try it in the load event or do it in a setTimeout
    2. I’ve never managed to get postMessage to work with anything other that ‘*’ as the origin. If I put a URL in there I get a security warning “Security Error: Content at http://xxx/ may not load data from http://yyy/“, except only in the error console (Ctrl + Shift + J) not in any other place. I suspect there’s some other stuff that needs set up somewhere for that to work, but I’ve not yet figured out what.

    Here’s a jsfiddle with a slightly modified version of your code loading a document off my domain, works for me in Firefox and Chrome.

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

Sidebar

Related Questions

I have a few lines of PowerShell code that I would like to use
I would like to know a few practical use-cases (if they are not related/tied
A few days ago I posted this question and everyone suggested me to use
I'm trying to use Python to concatenate a few javascript files together before minifying
I need a few lines of Javascript code that will take a hexadecimal number
I'm outputting a few lines of Javascript within a Repeater control on an ASPX
I have quite a few lines of code that create objects and using various
I have a few places where I use the line below to clear out
I use a form to run a few codes on a database in Access.
After a few harsh lessons I now always use OrdinalIgnoreCase when comparing Strings in

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.