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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:08:51+00:00 2026-06-03T00:08:51+00:00

I need to test out a situation where I have a parent window hosting

  • 0

I need to test out a situation where I have a parent window hosting an iframe which will be empty. I need to use the iframe as a proxy between the parent and a cross-domain resource. I’ve created a simple jsfiddle that should attempt to post a message from the parent to the child iframe.

What I try to do in the parent’s javascript is have a handler for the child iframe, but for the life of me I cannot figure out the syntax to get it working correctly. I have tried using an addEvent helper to get the event properly added (for IE and everybody else) from examples I’ve seen in Modern Javascript Develop and Design:

addEvent: function(obj, type, fn) {
    if (obj && obj.addEventListener) {
        obj.addEventListener(type, fn, false);
    } else if (obj && obj.attachEvent) {
        obj.attachEvent('on' + type, fn);
    }
}

The way I’m currently trying to setup the handler for onmessage is as follows:

document.getElementById("frame1").onmessage = function(e) {
    alert(e.data);
};

I have also thought that maybe I need to be accessing the element’s contentWindow, but onmessage does not seem to exist.

Is there a property or way to access an iframe such that it appears like a window and would have an onmessage that could be handled?

Update

I can see the value of onmessage set to a function and even alert(…) out the value to see that it is indeed my intended function, but I do not see it called when postMessage is called on the child iframe.

  • 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-03T00:08:54+00:00Added an answer on June 3, 2026 at 12:08 am

    Your implementation of postMessage is incorrect. The onmessage event has to be bound to the the frame’s window. Since the frame’s content is from a different origin, this can only be done at the frame’s side. You cannot bind this event from your page, due to the Same origin policy.

    This code will work, provided that the Same origin policy is not in affect:

    // Bind event to the frame's  window  object
    var frame1_window = document.getElementById("frame1").contentWindow;
    frame1_window.onmessage = function(e) {
        alert(e.data);
    };
    frame1_window.postMessage("Hello from same domain", "http://yourdomainhere.com");
    

    Example (domain: jsfiddle.net): http://jsfiddle.net/zTctZ/3/show/
    Example (domain: fiddle.jshell.net): http://jsfiddle.net/zTctZ/4/

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

Sidebar

Related Questions

Hi all I have this situation , I need to write unit test cases
I have a situation where I need to create a (arguably dirty) soak test
I have the following situation which I can't figure out. I have a menu
I have a situation where I need to mock a third-party interface and call
I have a situation where I need to know what methods are being called
I have large text files upon which all kinds of operations need to be
I need to create a test database out of a huge database where the
I need to test whether various types of database objects exist in a given
I need to test a serial port application on Linux, however, my test machine
I need to test a site with a dynamic menu in Mac Firefox, but

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.