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

  • Home
  • SEARCH
  • 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 7722909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:18:40+00:00 2026-06-01T04:18:40+00:00

This is WebKit browsers specific (meaning that I only need to make it work

  • 0

This is WebKit browsers specific (meaning that I only need to make it work in WebKit specific, i.e. iOS/Android browsers, but I’m testing in Chrome).

I have a page. The page loads one or more iframes, with contents from another domain. I need to receive messages (using postMessage()) from these iframes, and I need to be able to identify which iframe a specific message came from.

I can’t find a way to do that that does not involve throwing something the iframe URL that the iframe contents then can pass back to me. I would like to not have to meddle with the URL, as there is no guarantee I can safely do that (redirects can throw the parameters out, for example).

I tried something that I thought was reasonable. When I create the iframe element (it’s done from J/S), I associated a property with the element, let’s say ‘shared_secret’. When I get the message event back from the frame, I tried to locating the element that the calling frame was created with, and reading that property.

function onMessage(evt) {
  var callerId = evt.source.frameElement.shared_secret;
  // ....
}

window.addEventListener(message, onMessage);

var frameEl = document.createElement('iframe');
frameEl.shared_secret = 'sommething blue';
frameEl.src = 'http://aliens.com/my.html';
somewhereInMyDoc.appendChild(frameEl);

When the frame loads, it will run:

window.parent.postMessage('do you know who I am?', '*');

However, frameElement turns out undefined in the above onMessage(). I guess for the security reasons, it does work perfectly when the parent/child are from the same domain.

And it’s actually ironic. Parent window can not access event.source.frameElement because event.source is an alien window. iFrame window can not call window.frameElement, because frameElement is in an alien window. So nobody can get access to it.

So, is there something that I can use as a token that I can set on a newly loaded frame, and somehow get back?

Thank you.

  • 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-01T04:18:42+00:00Added an answer on June 1, 2026 at 4:18 am

    For people looking for some code, here is what I used to find the iframe who sent the message :

    /**
     * Returns the iframe corresponding to a message event or null if not found.
     * 
     * 'e' is the event object
     */
    function getFrameTarget (e) {
        var frames = document.getElementsByTagName('iframe'),
            frameId = 0,
            framesLength = frames.length;
    
        for (; frameId < framesLength; frameId++) {
            if (frames[frameId].contentWindow === e.source) {
                return frames[frameId];
            }
        }
    
        return null;
    }
    

    Thanks to Pawel Veselov and DMoses !

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

Sidebar

Related Questions

This problem exists for webkit browsers only chrome and possibly safari (i've not tested
This site loads fine in Firefox, but in WebKit browsers (Safari and Google Chrome)
I really like sql-database in HTML5, but so far only Opera and Webkit browsers
This hadn't hit me until now (and this is not only in webkit browsers).
In webkit browsers this page renders fine: http://www.ryanhaywood.com/s/film.html But in the updated firefox it
This issue is minor but is bugging me, why do webKit browsers render the
I know how to do this for Webkit browsers but I'm kinda stuck in
I have a very strange issue that only affects webkit browsers for some reason,
Would anyone now why this script is disabling my nav in webkit browsers $(document).ready(function
I am getting unexpected console output after array assignment in webkit browsers (Chrome 16.0.912.77

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.