Javascript 现在哪些浏览器支持 window.postMessage 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6036418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:58:41  来源:igfitidea点击:

What browsers support the window.postMessage call now?

javascriptcross-browser

提问by Jamey McElveen

What are all the browsers that support the window.postMessagecall now? I am looking for browsers that support it natively, not through an iFrame hack.

window.postMessage现在支持通话的浏览器都有哪些?我正在寻找原生支持它的浏览器,而不是通过 iFrame hack。

回答by Raynos

Can I use cross-document messaging

我可以使用跨文档消息传递吗

FF3+, IE8+, Chrome, Safari(5?), Opera10+

FF3+、IE8+、Chrome、Safari(5?)、Opera10+

回答by mags

postMessage is supported in IE8+ HOWEVER

但 IE8+ 支持 postMessage

  • Remember that IE9 and below require data to be passed in string form and not as an object.
  • IE doesn't like you to call postMessageas soon as page loads (I'm assuming this has to do with the iframe you are posting to needing time to load).
    Use a setTimeoutto wait one or two seconds before calling postMessage.
    It took me hours to figure this out and IE wasn't giving me any error message, it was just silently doing nothing until I added the setTimeout.
  • 请记住,IE9 及以下要求以字符串形式而不是对象形式传递数据。
  • IE 不希望您在postMessage页面加载后立即调用(我假设这与您发布的 iframe 需要加载时间有关)。在调用之前
    使用 asetTimeout等待一两秒钟postMessage
    我花了几个小时才弄清楚这一点,IE 没有给我任何错误消息,它只是默默地什么都不做,直到我添加了 setTimeout。

If you want to start with a demo which actually does work in IE, check out this nifty tutorial by Ilya Kantor

如果您想从实际在 IE 中运行的演示开始,请查看Ilya Kantor 的这个漂亮教程

回答by benipsen

For what it's worth recently I ran into some odd webkit browser/versions out in the wild that did NOT support postMessage. I was using IE(8) detection as my means for seeking an alternative. Instead, I probably should have just done some something like this:

对于最近的价值,我在野外遇到了一些不支持 postMessage 的奇怪的 webkit 浏览器/版本。我使用 IE(8) 检测作为寻找替代方案的手段。相反,我可能应该做一些这样的事情:

if(window.postMessage){
    console.log('Supports post message');
}

Or likely a bit cleaner:

或者可能更干净一点:

var pm_is_supported = typeof(window.postMessage) == 'function';

回答by Jay Shah

All latest browsers supports that e.g. IE 11, Edge, Firefox 57+, Dafari 11+, iOS Safari 10.2+, Opera mini, Chrome for android, UC Browser etc.

https://caniuse.com/#search=document%20messaging

所有最新浏览器都支持,例如 IE 11、Edge、Firefox 57+、Dafari 11+、iOS Safari 10.2+、Opera mini、Android 版 Chrome、UC 浏览器等

https://caniuse.com/#search=document%20messaging