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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:32:37+00:00 2026-05-27T11:32:37+00:00

So far I’ve only seen tutorials for postmessage where one window sends a single

  • 0

So far I’ve only seen tutorials for postmessage where one window sends a single kind of message, and the other window interprets the message in only a single way.

What if I want to have many different kinds of interactions between windows, can postmessage handle that?

Is that going against the grain of what postmessage is supposed to do?

For example, what if I wanted to be able to send custom callbacks back and forth, etc?

  • 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-27T11:32:37+00:00Added an answer on May 27, 2026 at 11:32 am

    There are a couple of ways to pass a multi-part message on to a postMessage handler. The first (and less “clean” way) is to use a delimiter character, then pass your data through a string.

    Let’s say we wanted to pass a user ID, an action, and the users name. The string would look like this:

    54|do_logout|chris

    Within the postMessage handler, the passed data can be split (docs) on the | character, then each segment of the message can be used as needed.

    Another route, instead of manually creating/splitting a string, is to use JSON (docs) to convert an object into a string on one side, and use JSON to convert back to an object in the handler.

    var pass_data = {
        'name':'Chris',
        'id':54,
        'action':'do_logout'
    };
    target_window.postMessage(JSON.stringify(pass_data), "http://www.example.net");
    

    … then in the handler:

    function (event) {
        var pass_data = JSON.parse(event.data);
    }
    

    Be sure to test, though, as the JSON object is not provided on all user agents, especially older ones. There are many (many, many) third-party libraries out there to shim JSON support, so don’t let the lack of complete adoption scare you away – JSON is definitely a safe “moving forward” standard.

    Wouldn’t it be nicer if we could just pass that object straightaway? Well, staring in Firefox 6 (source), the data you pass to a postmessage handler may be an object. The object will be serialized, so there are some concerns on that front, but:

    var pass_data = {
        'name':'Chris',
        'id':54,
        'action':'do_logout'
    };
    target_window.postMessage(pass_data, "http://www.example.net");
    

    A little nicer, eh? Unfortunately, current versions of IE will only deal with strings. I was not able to find any discussion on future plans regarding postMessage for IE 10. Further, there is a known bug in IE 8/9 which breaks postMessage for anything other than frames. (source).

    Getting in to a specific aspect of your question – callbacks. Unless you’re able to pass the callback by function name, there isn’t a way to pass a function; no anonymous functions for you. This is related to the way the data is actually passed on to the handler. In practice, there “is not” support for objects as data, behind the scenes the browser is turning your passed object into a string (serialization).

    All that said, then, you should understand that passing an object is exactly the same as using JSON to stringify an object before passing, only in the former case the browser is doing its own serialization (and subsequent unserialization), whereas with the latter route it is up to you to serialize/unserialize.

    The take-away points here:

    • postMessage still has limited cross-browser support
    • The trend for newer versions of standards-compliant browsers is to allow passage of objects in addition to strings
    • The passed object will be serialized, so no function references allowed
    • The widest support “in the wild” is for string-only data, which means you’ll have to stick with strings and “pack” your data as demonstrated above if you want to support a wide variety of user agents
    • Internet Explorer will ruin every plan you ever make (including family holidays)

    Documentation and References

    • window.postMessage: https://developer.mozilla.org/en/DOM/window.postMessage
    • String.split: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split
    • IE 8/9 HTML5 support notes: http://blogs.msdn.com/b/ieinternals/archive/2009/09/16/bugs-in-ie8-support-for-html5-postmessage-sessionstorage-and-localstorage.aspx
    • JSON.stringify: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify
    • JSON.parse: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/parse
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I have been using in tutorials, examples and sample code only packages
So far the only way I have been able to keep index.yaml updated when
So far I only found two ways: throw exception play nothing ad.2. Pretty recent
So far I know two ways to pass php variables to javascript. One is
As far as I know, in C++ one can declare the same name multiple
As far as I know, strings are immutable in Delphi. I kind of understand
As far as i know it's impossible to achieve the following, but only an
Far as best practices are concerned, which is better: public void SomeMethod(string str) {
As far as I know, foreign keys (FK) are used to aid the programmer
So far I have encountered adjacency list, nested sets and nested intervals as models

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.