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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:08:27+00:00 2026-06-05T20:08:27+00:00

I have this code inside a iframe: window.addEventListener(‘message’, function(e){ if(e.data == ‘test’) console.log(e); },

  • 0

I have this code inside a iframe:

window.addEventListener('message', function(e){

  if(e.data == 'test')
    console.log(e);

}, false);

and this inside the parent document:

$('#the_iframe').get(0).contentWindow.postMessage('test', 'http://localhost/');

So the parent document sends a “test” message to the iframe and it works.

But how can I define a function in the parent document, and somehow send this function through postMessage to the iframe, which will execute the function locally?

The function does some changes to the document like this:

var func = function(){
  $("#some_div").addClass('sss');
}

(#some_div exists in the iframe, not the parent document)

  • 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-05T20:08:31+00:00Added an answer on June 5, 2026 at 8:08 pm

    There’s nothing that would prevent you from passing a stringified function as postmessage event data. Implementation is trivial, for any function declaration like

    function doSomething(){
        alert("hello world!");
    }
    

    You could encodeURI its string interpretation:

    console.log(encodeURI(doSomething.toString()));
    //function%20doSomething()%20%7B%0A%20%20%20%20alert(%22hello%20world!%22);%0A%7D
    

    It can then be executed as part of a closure – something not overly imaginative like

    eval('('+decodeURI(strCallback)+')();');
    

    There’s a fiddle’d proof of concept without the cross-frame architecture – I’ll see if I can put together a postMessage version, but it would be non-trivial to host w/jsfiddle

    Update

    As promised, a full mockup that works (links below). With correct event.origin checks this would be sufficiently inpenetrable, but I know for the fact that our security team would never let eval into production like this 🙂

    Given the option I’d suggest the functionality be normalized across the two pages so that only a parametric message would need to be passed (i.e. pass arguments not functions); however there are definitely a few scenarios where this is a preferred approach.

    Parent code:

    document.domain = "fiddle.jshell.net";//sync the domains
    window.addEventListener("message", receiveMessage, false);//set up the listener
    
    function receiveMessage(e) {
        try {
            //attempt to deserialize function and execute as closure
            eval('(' + decodeURI(e.data) + ')();');
        } catch(e) {}
    }
    

    Iframe code:

    document.domain = "fiddle.jshell.net";//sync the domains
    window.addEventListener("message", receiveMessage, false);//set up the listener
    
    function receiveMessage(e) {
        //"reply" with a serialized function
        e.source.postMessage(serializeFunction(doSomething), "http://fiddle.jshell.net");
    }
    
    function serializeFunction(f) {
        return encodeURI(f.toString());
    }
    
    function doSomething() {
        alert("hello world!");
    }
    

    Prototype mockup: parent code and iframe code.

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

Sidebar

Related Questions

I have this code snippet inside a function that checks if an object exists
I have this code inside a header (edited): template <int i> class A {};
I have this code inside thread and when I call terminate it terminates fast
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,
I have this code that shows all the images inside 'images' directory but it
I have this code which will include template.php file from inside each of these
I have a jsp page which needs a java code. This java code inside
I have a list of <p> inside a <div id=someDiv> I use this code
I have a page with an iframe. Inside the iframe is code (that I

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.