On javascript postmessage to parent HTML iframe that is on local disk

萝らか妹 提交于 2019-12-10 20:09:12

问题


I am working on a project that involves hosting a webpage in a iframe, while the hosting parent iframe is in a HTML file on local disk, say on c:\; while the inner hosted iframe is on some server. The two webpages need to do postmessage to each other.

There is no problem for the parent iframe (on local disk) to postmessage to the inner frame, since it knows the domain of the inner iframe;

But when the inner iframe need to postmessage back to the parent iframe, it need to provide the domain of the parent iframe which is on local disk.

I tried the following in the inner HTML to postmessage to its parent IFrame, which is on local disk:

*var messageToSend = {
                        jsonrpc: "2.0",
                        result: [result]
                    };
window.parent.postMessage(JSON.stringify(messageToSend), "file://");*

// window.parent IFrame is a HTML file that is on Local disk

But when I try using "file:" as the domain in postmessage back to the parent iframe, I am getting a "Invalid argument" error.

Can some one help? What should the domain of "local disk html" be? Any hints will be highly appreciated.

Cheers


回答1:


I don't know how to specify "file", I'm guessing there is no way because it doesn't make much sense. The purpose of the origin is to prevent others (presumably malicious) pages from seeing your message, but if the origin is just any file on the hard disk, anyone could see it by creating an html file with a frame and loading your web page from your server. So specifying "file" (even if there were a way) is no more secure than specifying " * ", so just use " * " and save yourself the headache.



来源:https://stackoverflow.com/questions/5466376/on-javascript-postmessage-to-parent-html-iframe-that-is-on-local-disk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!