Detect whether postMessage can send objects?

前端 未结 4 804
灰色年华
灰色年华 2020-12-29 00:30

I\'m looking for a neat way to detect whether postMessage in the browser supports the sending and receiving of objects or just strings. I figure that someone out there must

4条回答
  •  一个人的身影
    2020-12-29 00:51

    I wanted to know the same thing. I created this script to detect if an object could be passed in postMessage by a simple callback to the current window. You will see IE 9 return false, IE 10 returns true.

    http://jsfiddle.net/milesplit/DvqqH/

    var supportsPostObject = false;
    
    (function(){
        var callback = function(e) {
            supportsPostObject = (typeof(e.data)!='string');
        };
        (window.addEventListener) ?
            window.addEventListener('message', callback) :
            window.attachEvent('onmessage', callback);
        ('postMessage' in window) && window.postMessage({}, '*');
    })();
    
    setTimeout(function(){
       alert(supportsPostObject);
    }, 0);
    

提交回复
热议问题