JS 在open打開的子窗口頁面中調用父窗口頁面的JS方法


需求的情景如下:

1:做新增或修改等操作的時候打開一個新的瀏覽器窗口(使用window.open(參數等)方法)

2:在新增或修改等的頁面上有返回按鈕、重置按鈕、保存按鈕,對於返回就直接關閉此窗口(使用window.close()方法)重置就不說啦!對於保存的操作要保存好對應的信息,調用父窗口頁面的方法,關閉子窗口頁面

我們采用的方法的關鍵點如下句代碼(寫在ACTION類中的對應的方法中,通常放在最后邊):

renderHtml(getResponse(), "<script language=JavaScript>"+"window.opener.searchMethod();window.close();"+"</script>");

注:searchMethod()是父窗口頁面的方法,renderHtml()/getResponse()方法如下面所示

/**
* 直接輸出純HTML
*/
public void renderHtml(HttpServletResponse response, String text) {
PrintWriter out = null;
try {
out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.print(text);
} catch (IOException e) {

} finally {
if (out != null) {
out.flush();
out.close();
out = null;
}
}
}

/**
* 獲取response
*
* @return
*/
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}

參考如下:

1:JS刷新父窗口的幾種方法

http://www.cnblogs.com/top5/archive/2010/10/18/1854666.html

2:JS 在open打開的窗口中調用父頁面JS方法

http://jxdwuao.iteye.com/blog/793234


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM