Detect whether postMessage can send objects?

前端 未结 4 793
灰色年华
灰色年华 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:40

    postMessage also works between iframes; assuming that the behavior is the same between workers and frames, you should try the following or something like it:

    
       
          
          
          
    
    

    You may need to replace console or console.log to be able to see results, but on Chrome, this gets me

    listener attached about:blank (1):1
    asdf about:blank (1):1
    Object {whatAmI: "an object, maybe?"} about:blank (1):1
    

    when I save it to a local file and open it up.

    The jsfiddle version (and the version which uses an actual worker) are left as an exercise for the reader. :)

提交回复
热议问题