【发布时间】:2021-01-28 18:07:48
【问题描述】:
我正在使用 $postMessage 与父窗口进行跨浏览器通信。 除 Safari 外,所有浏览器都可以正常工作。问题是使用 document.location.hash 哈希在 Safari 中返回为空。 无论如何,这发生在 Safari 中。
function postMessage( msg ){
var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) );
$.postMessage({ method:'resize', message:msg}, parent_url, parent );
}
更新:
因此,父窗口正在创建一个 Iframe,该 Iframe 将 '#url 附加到它的末尾。我正在使用 document.location.hash.replace( /^#/, '' ) 来获取该 url 以用作 postMessage 的 url。在 Safari V8.0.2 中测试所有这些时,Safari 删除了 # 和 # 之后的任何内容。所以我的 parent_url 返回空白。
解决方案是在 url 末尾使用 ¶m=url 而不是 #。 我不确定为什么 Safari 会删除 # 或者它是否是一个有据可查的问题。我一直在网上搜索以了解更多信息。
【问题讨论】:
-
不工作以什么方式?抛出错误?
-
window.location.hash,不要使用document -
document.location.hash 在 safari 中返回为空
-
那么把它改成window有什么不同吗?
-
window 在 safari 中产生相同的结果。
标签: javascript jquery safari postmessage