1

I have a page that listens to messages from a page on another domain (via an iframe) using window.postMessage. My previous code works:

<script type="text/javascript">
    window.addEventListener("message", receiveMessage, false);

    function receiveMessage(event) {
      if (event.data == "scrollTop"){
          window.scrollTo(0,0);
      }
    }
</script>

Even though the above code works, I need to restrict it from executing except when the .postMessage is from a specific domain (http://origindomain.com), so I changed the above code to the following:

<script type="text/javascript">
    window.addEventListener("message", receiveMessage, false);

    $(function () {
        $.receiveMessage(function(event){  
            if (event.data == "scrollTop") {
                window.scrollTo(0, 0);
            }

            // An optional origin URL (Ignored where window.postMessage is unsupported).
            }, 'http://origindomain.com' );
        });
    });
</script>

This second approach doesn't work. The error output in Firebug console is:

ReferenceError: receiveMessage is not defined
window.addEventListener("message", receiveMessage, false);

jQuery is not my first language, so if the solution is obvious I apologize. Why is the receiveMessage() callback undefined?

5
  • well.. you never defined it. You defined a $.receiveMessage, but not a receiveMessage. Commented Jan 23, 2014 at 18:58
  • You're specifying the callback function receiveMessage by name, but you have not defined such a function. Commented Jan 23, 2014 at 18:59
  • 1
    You are declaring receiveMessage on jQuery document ready handler, while you are attaching it to the window event listener outside of that scope Commented Jan 23, 2014 at 19:03
  • Daniel, your comment taught me something. Thank you for spelling it out! Commented Jan 23, 2014 at 19:06
  • Glad it helped you out! Commented Jan 23, 2014 at 19:11

1 Answer 1

4

You never defined receiveMessage, instead you tried to use $.receiveMessage. You're miss-using the jquery methods, you don't always need to wait for the dom to be ready. You also don't need jquery at all for this.

<script type="text/javascript">
    window.addEventListener("message", receiveMessage, false);

    //$(function () {
    function receiveMessage(event){  
        if (event.origin == "http://origindomain.com" && event.data == "scrollTop") {
            window.scrollTo(0, 0);
        }
    }
    //});
</script>

If you installed a jQuery plugin that defines $.receiveMessage, then you likely didn't need the first line at all because the plugin should take care of that. But, i'm not sure why a plugin would be needed for this functionality.

Sign up to request clarification or add additional context in comments.

5 Comments

Kevin, your code works and Daniel's comment above helps me understand why mine didn't. Thank you.
@Kevin is right, nothing in this code needs to be on document ready, so this way is better in terms of performance!
Kevin, I am using a plugin from benalman.com/projects/jquery-postmessage-plugin - it would be nice to eliminate the dependency if it isn't needed. As I said, Neither jQuery nor Javascript are my first language. Perhaps this plugin was recommended to support older browsers?
postMessage is an HTML5 function, I believe. The plugin is used for browsers that do not support HTML5. I think this is correct...
But, it's supported all the way back to IE8. you only need it if you need to support IE6/7 and older versions of opera. I generally would agree with adding backwards IE support, but for me that stops at IE8 since IE7 has fallen well below 1%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.