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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:27:29+00:00 2026-05-26T16:27:29+00:00

I am using post message to adjust the height of an iframe dynamically. The

  • 0

I am using post message to adjust the height of an iframe dynamically. The html that is holding the iframe adds a listener that will trigger the frame resize. The (simplified) relevant code on the parent looks something like this:

<div id="dynamic_content">
    <iframe  src="http://my.content"></iframe>-->
</div>

<script>
    window.onload = function () {
        window.addEventListener("message", handleEventTrigger, false);
    };

    function handleEventTrigger(evt) {
        var height = evt.data;
        alert("Received: " + evt.data);
        if (isNumber(height)) {
            $("#dynamic_content").height(height + "px");
        }
    }

    function isNumber(str) {
        return !isNaN(str - 0);
    }    
</script>

On the source for the iframe I trigger a postMessage when it loads. Basically I have a div with id outerDiv which I want to send the height of. The relevant javascript code is:

window.onload = function () {
    var height = $("#outerDiv")[0].offsetHeight;
    if (isNumber(height)) {
        parent.postMessage(height, "*");   
    }        
};

My problem now is that this only works occasionally. I am assuming there is a race condition between adding the event listener and firing the post message. I rely on having bound the event listener before the post message is fired. What is the appropriate way to ensure this? Both are bound to window.onload. Are these addressing the same window, and is this a/the problem?

  • 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-26T16:27:29+00:00Added an answer on May 26, 2026 at 4:27 pm

    From what I understand the code is executed in the order it is printed, so I solved this by making sure the addEventListener is placed before the <iframe> in the code – and also stop doing this onload:

    <script>
        window.addEventListener("message", handleEventTrigger, false);        
        function handleEventTrigger(evt) { ... }
    </script>
    
    <iframe  src="http://my.content"></iframe>-->
    

    This solves the problem, and my race condition is gone. However, from what I’ve understood the recommendation is to place the js-code at the end of the html. If there is a good solution where I can achieve this too please shout out..

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

Sidebar

Related Questions

when using http post with flash that send ip address request from client computer
I'm using the following to post a message on my Facebook page: $attachment =
im using the js sdk to post message to friends wall from my website
I'm building Blackberry application, and I need to post message to MySpace using oAuth.
I have a form which submits user first and last name using POST signin.html
i am creating a iphone application using Facebook SDK.I want to post message to
When using simple request to post to Twitter the messages are signed with sent
Background I'm using POST form submissions instead of links with concatenated arguments inside a
In my application am submiting my form by using post for a php page.
Just started with JQuery and I've managed to pass a parameter using POST, firebug

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.