使用跨源文档获取 showModalDialog 结果
我们的网站使用 showModalDialog
。根据对话框中所做的操作,我们可能希望也可能不希望重新加载打开对话框的页面。我们通过让对话框 JavaScript 将 window.returnValue
设置为 true
或 false
来实现这一点,然后底层页面在返回值中检查这一点对 showModalDialog
的调用。
当对话框文档和底层页面从同一源域加载时,这种方法可以正常工作。与网络中的典型情况一样,当对话框的域与页面的域不匹配时,showModalDialog
的返回值始终为 undefined
。
我喜欢以不同的方式将标志从对话框传递到底层窗口。幸运的是,我们的目标浏览器都支持 postMessage
,但对话框 JavaScript 似乎没有获取 window.opener
的值,所以我认为不会它可以获得对窗口的引用,以便它可以发布消息。在我们的代码库中实现iframe 解决方法是不可行的,因为它需要我们在多个站点上安装 iframe 收件人文档,这些站点都可以打开同一个对话框。
有没有更好的方法让我们可以通过对话框与跨域打开的页面进行通信,而无需重新加载它们?
Our web site makes use of showModalDialog
. Based on what is done within the dialog, we may or may not wish to reload the page that opened the dialog. We do this by having the dialog JavaScript set window.returnValue
to true
or false
, and then the underlying page inspects that in the return value of the call to showModalDialog
.
This works fine when the dialog document and underlying page are loaded from the same origin domain. As is typical with the web, when the dialog's domain doesn't match the page's, the return value from showModalDialog
is always undefined
.
I've entertained passing the flag from the dialog to the underlying window in different ways. Fortunately, we're targeting browsers that all support postMessage
, but the dialog JavaScript doesn't appear to be getting a value for window.opener
, so I don't think it can get a reference to the window so it can post a message. Implementing an iframe workaround like this is unfeasible with our code base because it would require us to install iframe recipient documents on multiple sites that can all open this same dialog.
Is there a better way for us to have a dialog communicate with cross-origin opening pages without requiring that they reload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能已经找到了自己的答案。据我了解,如果我将打开文档和对话框文档的
document.domain
属性设置为相同的值,我应该能够访问此信息。由于两者都在同一根域名的子域内,因此它应该可以工作。如果我尝试这个并且有效,我会接受这个答案。I may have found my own answer. From what I understand, if I set the
document.domain
property for both the opener document and the dialog document to the same value, I should be able to access this information. Since both are within subdomains of the same root domain name, it should work. If I try this and it works, I will accept this answer.