我有一个jsp,其中负责一个属于不同域的iframe。在该 iframe 内有一个 JSP,它导航到一个 servlet,该 servlet 又导航到另一个 JSP。
我想要的是从 iframe 第二个 JSP(必须这样做,因为它们是独立的 Web 应用程序)对主 JSP(其中加载了 iframe)进行更改。
我尝试从加载的 JSP 中加载 window.top、parent 和所有引用 Servlet,但在其他情况下都获得了访问拒绝响应。
有什么选择可以做到这一点还是我必须重新考虑一切?
总结:
....
<br>< div id="xxxx">< /div>
<br>< div id="yyyy">
<br>< iframe src=(other domain)><br>
Here I load a webpage. it navigates to a servlet and to a second JSP. This is the JSP to do the inner.html in the "xxxx" div
<br>< /iframe>
<br>< /div>
....
编辑: 欲了解更多信息:
1.- window.top.getElementById("xxxx")... 出现错误(不接受 getElementById) 2.- window.top.location.href = "http://www.google.com ";工作正常
最佳答案
您可以在窗口框架之间使用消息。 iframe 中的内容将向父框架发送消息,父框架必须显式接收该消息并采取操作。
消息使用发送
parentWindowObject.postMessage(message, targetOrigin);
使用接收
addEvent(window, "message", function(e){
console.log(e.data);
});
这样您就可以发送带有父框架应执行操作的说明的对象。请注意,不建议发送 JS 进行评估。
<小时/>您可以在此处阅读有关在窗口之间发送/接收消息的更多信息: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
页面摘录:
The window.postMessage method safely enables cross-origin communication. Normally, scripts on different pages are allowed to access each other if and only if the pages that executed them are at locations with the same protocol (usually both https), port number (443 being the default for https), and host (modulo document.domain being set by both pages to the same value). window.postMessage provides a controlled mechanism to circumvent this restriction in a way which is secure when properly used.
关于javascript - 从框架引用窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528831/