0

I've got a webpage that won't work properly if postMessage is not available. I'd like to use a simple:

if (window.postMessage === undefined) {
  //alert the user
}

Are there any unforeseeable issues with this approach?

Eric
  • 1,209
  • 1
  • 17
  • 34
  • well someone could do `window.postMessage = "HAHA";` :) – epascarello May 22 '13 at 16:37
  • 1
    And it will not be `"undefined"` if would be `undefined`. You are checking for a string! – epascarello May 22 '13 at 16:38
  • 2
    If the issues are "unforeseeable", how could we know about them? –  May 22 '13 at 16:41
  • I just edited it to be undefined without the quotes. And, as someone who is using postMessage for the first time, the window.postMessage is checked on loading the page. Could a user change it to "HAHA" if I only check once upon loading? – Eric May 22 '13 at 16:42
  • 1
    @Eric: Yes, a user could do that, but then users that do that would only be harming themselves. In modern browsers, you can make a property not writeable. `Object.defineProperty(window, "postMessage", {value: function() {...}});` –  May 22 '13 at 16:44
  • I agree. Would it be easy to explain how they could do that if my check occurs as soon as the page loads? (More for my info: if they want to do that, they'll actually have more of a problem than I would!) – Eric May 22 '13 at 16:45
  • 1
    @Eric: After the page is loaded, they could just open the browser's developer tools and type `window.postMessage === null` in the console. –  May 22 '13 at 16:47
  • OK! I wasn't aware that one could do that. Is this true for all javascript variables or just window object variables? – Eric May 22 '13 at 16:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30431/discussion-between-eric-and-squint) – Eric May 22 '13 at 16:53

2 Answers2

1

I'm not a JS expert, nor a fan, but heres my experience with a similar problem:

We had a similar problem: We use "postMessage" on an object to communicate with a native library loaded in the chrome browser. At some point, "postMessage" was undefined after it was working fine for communication.

It turns out it arose after we toggled the visibility of the div that contained the html object on which we were calling "postMessage" on.

We had something like:

In our case we were calling "postMessage" on the "embed" object (this is how communication with the native layer is being done).

So, if it is a similar scenario, maybe you have modified some properties of the "window" object which in turn made the "postMessage" function undefined. It's just a hint.

Dan Borza
  • 3,419
  • 3
  • 22
  • 16
0
    function handlingMsg(e){
        var heightIn = (typeof e.data === "object") ? e.data['height'] : e.data;
        $("#zbFrame").attr("height",Number(heightIn));
    }
    //iframe 적용
    // <= ie8
    if (!window.addEventListener) {
        window.attachEvent("onmessage", handlingMsg);
    }else{ // > ie8
        window.addEventListener('message', handlingMsg, false);
    }