在GWT-app中,如何使用doPost的响应关闭IFrame

时间:2012-02-16 07:53:17

标签: javascript gwt iframe

在我的GWT应用程序中,我通过Recurly收到付款,其中Recurly的页面位于IFrame中。当Recurly成功处理付款后,他们的页面会重定向到我提供的成功网址,该网址会使用POST点击我的servlet。收到POST后,我想关闭IFrame并对我的小部件执行操作。我在回复中使用javascript关闭IFrame:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    /* other code */
    StringBuilder buf = new StringBuilder();
    buf.append("<script language=\"javascript\" type=\"text/javascript\">");
    buf.append("var n = window.parent.document.getElementById(\"recurly-popup\");");
    buf.append("n.parentNode.removeChild(n);");
    buf.append("</script>");
    response.getOutputStream().print(buf.toString());
}

但是,当IFrame关闭时,我似乎无法在客户端上找到任何检测方法。如何使用GWT或GWTQuery检测它?

1 个答案:

答案 0 :(得分:0)

您可以尝试添加Javascript侦听器(通过GWT的JSNI)。这里记录了FF和IE浏览器所需的监听器:http://bytes.com/topic/javascript/answers/627389-having-trouble-event-listener-detect-iframe-close-event-fromparent

在删除iframe之前,另一个需要考虑的选项可能是在父级上执行javascript函数(例如发送事件)。类似的东西:

buf.append("<script language=\"javascript\" type=\"text/javascript\">");
buf.append("window.parent.document.myGWTJSNIFunction('recurly_processed_event');");
buf.append("var n = window.parent.document.getElementById(\"recurly-popup\");");
buf.append("n.parentNode.removeChild(n);");
buf.append("</script>");

如果Javascript不正确,则道歉,尚未经过测试。有关GWT JSNI的更多详细信息,请访问:http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html