javascript封装到类里面的方法

/**
  * 在页面上弹出窗口
  * @param showModal 是否弹出模式窗口。如果为true则弹出模式窗口;否则弹出非模式窗口。
  * @param refreshParent 当关闭弹出的模式窗口时,是否刷新父窗口。如果为true则刷新父窗口;否则不刷新父窗口。该参数只对弹出模式窗口有效。
  * @param url 弹出窗口的URL
  * @param feature 弹出窗口的特征。特征值间用分号分隔,可设置的特征值有:
  *      dialogHeight
  *     dialogLeft
  *         dialogTop
  *         dialogWidth
  *         center:{ yes | no | 1 | 0 | on | off }
  *         dialogHide:{ yes | no | 1 | 0 | on | off }
  *         edge:{ sunken | raised }
  *         help:{ yes | no | 1 | 0 | on | off }
  *         resizable:{ yes | no | 1 | 0 | on | off }
  *         scroll:{ yes | no | 1 | 0 | on | off }
  *         status:{ yes | no | 1 | 0 | on | off }
  *         unadorned:{ yes | no | 1 | 0 | on | off }
  * @throws IOException
  */
public void showDialog(boolean showModal, boolean refreshParent, String url, String Width,String Height) throws IOException {
  FacesContext ctx = FacesContext.getCurrentInstance();
     String contentType = "application/octet-stream;charset=gb2312";
     HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
     response.setContentType(contentType);
     ServletOutputStream out = response.getOutputStream();
     StringBuffer buf = new StringBuffer();
     buf.append("<base target=\"_self\">\n");
     buf.append("<script type=\"text/javascript\">\n");
     buf.append("<!--\n");
     if (showModal) {
      //弹出模式窗口
      buf.append("var rtn=window.showModalDialog('");      
     } else {
      //弹出非模式窗口
      buf.append("var rtn=window.showModelessDialog('");
     }
     buf.append((url == null || url.length() == 0)?"":url+"?"+System.currentTimeMillis());
     buf.append("', 'showModel' ,'");
     buf.append("dialogTop:200px;dialogLeft:300px;dialogHeight:"+Height+"px;dialogWidth:"+Width+"px;status:no;resizable:no");
     buf.append("');\n");
     if (showModal && refreshParent) {
      //模式窗口关闭后,刷新父窗口,要强制刷新
      //buf.append("if(rtn!=null){\n");
      //buf.append("window.parent.location.href=window.parent.location.href;\n");
      //buf.append("alert(window.parent);\n");
      //buf.append("window.parent.location.href=window.parent.location.href;\n");
      buf.append("window.parent.document.getElementById(\"hiddenReloadLink\").click();\n");
      //buf.append("location.href=window.dialogArguments;\n");
      //buf.append("}\n");
     }
     buf.append("window.document.location.href=window.parent.hiddenReloadLink.href;\n");
     buf.append("//-->\n");
     buf.append("</script>");  
     out.print(buf.toString());
     out.flush();
     ctx.responseComplete();
}

/**
  * 适用弹出一个摩态子窗口,选择保存以后关闭,父窗口跳转到下一个页面
  * 适合场合:下个页面是预先知道,而且固定的
  * @param dialogUrl 弹出窗口地址
  * @param destinationUrl 子窗口关闭后,父窗口跳转到的地址
  * @param Width 弹出窗口宽度
  * @param Height 弹出窗口高度
  * @throws IOException
  */
public void showDialogAndTurnOnePage(String dialogUrl,String destinationUrl, String Width,String Height) throws IOException {
  FacesContext ctx = FacesContext.getCurrentInstance();
     String contentType = "application/octet-stream;charset=gb2312";
     HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
     response.setContentType(contentType);
     ServletOutputStream out = response.getOutputStream();
     StringBuffer buf = new StringBuffer();
     buf.append("<base target=\"_self\">\n");
     buf.append("<script type=\"text/javascript\">\n");
     buf.append("<!--\n");
     //弹出模式窗口
     buf.append("var rtn=window.showModalDialog('");      
     buf.append((dialogUrl == null || dialogUrl.length() == 0)?"":dialogUrl+"?"+System.currentTimeMillis());
     buf.append("', '' ,'");
     buf.append("dialogTop:200px;dialogLeft:300px;dialogHeight:"+Height+"px;dialogWidth:"+Width+"px;status:no;resizable:no");
     buf.append("');\n");
     //模式窗口关闭后,父窗口跳转到destinationUrl
     buf.append("if(rtn!=null){\n");
      //buf.append("window.parent.location.href=\""+destinationUrl+"\";\n");
      buf.append("window.parent.document.getElementById(\"hiddenReloadLink\").href=\""+destinationUrl+"\";\n");
      buf.append("window.parent.document.getElementById(\"hiddenReloadLink\").click();\n");
     buf.append("}\n");
     buf.append("window.document.location.href=window.parent.hiddenReloadLink.href;\n");
     buf.append("//-->\n");
     buf.append("</script>");  
     out.print(buf.toString());
     out.flush();
     ctx.responseComplete();
}

/**
  * 适用弹出一个摩态子窗口,选择保存以后关闭,父窗口跳转到下一个页面,下个页面来源返回值
  * @param dialogUrl
  * @param Width
  * @param Height
  * @throws IOException
  */
public void showDialogAndTurnNextPage(String dialogUrl,String Width,String Height) throws IOException {
  FacesContext ctx = FacesContext.getCurrentInstance();
     String contentType = "application/octet-stream;charset=gb2312";
     HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
     response.setContentType(contentType);
     ServletOutputStream out = response.getOutputStream();
     StringBuffer buf = new StringBuffer();
     buf.append("<base target=\"_self\">\n");
     buf.append("<script type=\"text/javascript\">\n");
     buf.append("<!--\n");
     //弹出模式窗口
     buf.append("var rtn=window.showModalDialog('");      
     buf.append((dialogUrl == null || dialogUrl.length()== 0)?"":dialogUrl+"?"+System.currentTimeMillis());
     buf.append("', '' ,'");
     buf.append("dialogTop:200px;dialogLeft:300px;dialogHeight:"+Height+"px;dialogWidth:"+Width+"px;status:no;resizable:no");
     buf.append("');\n");
     //模式窗口关闭后,父窗口跳转到destinationUrl
     buf.append("if(rtn!=null){\n");
     // buf.append("window.parent.location.href=rtn;\n");
     buf.append("window.parent.document.getElementById(\"hiddenReloadLink\").href=rtn;\n");
     buf.append("window.parent.document.getElementById(\"hiddenReloadLink\").click();\n");
     buf.append("}\n");
     buf.append("window.document.location.href=window.parent.hiddenReloadLink.href;\n");
     buf.append("//-->\n");
     buf.append("</script>");  
     out.print(buf.toString());
     out.flush();
     ctx.responseComplete();
}

/**
  * 关闭窗口,没有定义父窗口跳转
  * @throws IOException
  */
public void closeWindow() throws IOException
{
   FacesContext ctx = FacesContext.getCurrentInstance();
       String contentType = "application/octet-stream;charset=gb2312";
       HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
       response.setContentType(contentType);
       ServletOutputStream out = response.getOutputStream();
       out.print("<base target=\"_self\">");
       out.print("<form name=\"form1\">\n");
       out.print("<input type=\"submit\" onclick=\"javascript:window.returnValue='ok';window.close();\" name=\"close\"/>\n");
       out.print("</form>\n");
       out.print("<script type=\"text/javascript\">\n");
       out.print("<!--\n");
       out.print("document.form1.close.click();\n");
       out.print("//-->\n");
       out.print("</script>");
       out.flush();
       ctx.responseComplete();      
}

/**
  * 关闭并且父窗口跳转到下个页面
  * @param dialogUrl
  * @param destinationUrl
  * @param Width
  * @param Height
  * @throws IOException
  */
public void closeAndReturnNextPage(String destinationUrl) throws IOException {
    FacesContext ctx = FacesContext.getCurrentInstance();
       String contentType = "application/octet-stream;charset=gb2312";
       HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
       response.setContentType(contentType);
       ServletOutputStream out = response.getOutputStream();
       out.print("<base target=\"_self\">");
       out.print("<form name=\"form1\">\n");
       out.print("<input type=\"submit\" onclick=\"javascript:window.returnValue='"+destinationUrl+"';window.close();\" name=\"close\"/>\n");
       out.print("</form>\n");
       out.print("<script type=\"text/javascript\">\n");
       out.print("<!--\n");
       out.print("document.form1.close.click();\n");
       out.print("//-->\n");
       out.print("</script>");
       out.flush();
       ctx.responseComplete();
}

/**
  * 弹出窗口并等待返回值,返回以后更新页面值(例如放大镜)
  * @param dialogUrl 弹出的窗口地址
  * @param Width 宽
  * @param Height 高
  * @param param 返回需要更新的对象,多个以逗号分割。
  * @throws IOException
  */
public void showAndWaitReturn(String dialogUrl,String Width,String Height,String param) throws IOException {
  FacesContext ctx = FacesContext.getCurrentInstance();
     String contentType = "application/octet-stream;charset=gb2312";
     HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
     response.setContentType(contentType);
     ServletOutputStream out = response.getOutputStream();
     StringBuffer buf = new StringBuffer();
     buf.append("<script type=\"text/javascript\">\n");
     buf.append("<!--\n");
     //弹出模式窗口
     buf.append("var rtn=window.showModalDialog('");      
     buf.append((dialogUrl == null || dialogUrl.length() == 0)?"":dialogUrl);
     buf.append("', '' ,'");
     buf.append("dialogTop:200px;dialogLeft:300px;dialogHeight:"+Height+"px;dialogWidth:"+Width+"px;status:no;resizable:no");
     buf.append("');\n");
     //模式窗口关闭后,父窗口跳转到destinationUrl
     buf.append("if(rtn!=null){\n");
    
     for(int i=0;i<param.split(",").length;i++)
     {
      String[] params=param.split(",");
      buf.append(" replace(\""+params[i]+"\",rtn."+params[i]+");\n");      
     }
     buf.append("}\n");
     buf.append("function replace(param,paramvalue) \n");
          buf.append("{\n");
     buf.append(" for(var k=0;k<window.parent.document.all.length;k++)\n");
     buf.append(" {\n");
     buf.append("  if(window.parent.document.all(k).name!=\"\"&&window.parent.document.all(k).name!=null&&window.parent.document.all(k).value!=null)\n");
     buf.append("   {\n");
     buf.append("    var name=window.parent.document.all(k).name;\n");
     buf.append("       if(name.lastIndexOf(param,name.length)==name.length-param.length&&name.length-param.length!=-1)\n");
     buf.append("     {\n");
     buf.append("     window.parent.document.all(k).value=paramvalue;\n");
     buf.append("     }\n");
     buf.append("   }\n");
     buf.append(" }\n");
     buf.append("}\n");
    
     buf.append("window.document.location.href=window.parent.hiddenReloadLink.href;\n");
     buf.append("//-->\n");
     buf.append("</script>");  
     out.print(buf.toString());
     out.flush();
     ctx.responseComplete();
}

/**
  * 关闭窗口并返回一个object对象
  * @param param 要返回的参数列表,多个以“,”分割
  * @throws IOException
  */
public void closeAndReturnObject(String param,String paramValue) throws IOException {
    FacesContext ctx = FacesContext.getCurrentInstance();
       String contentType = "application/octet-stream;charset=gb2312";
       HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
       response.setContentType(contentType);
       ServletOutputStream out = response.getOutputStream();
       StringBuffer buf = new StringBuffer();
       //out.print("<input type=\"submit\" onclick=\"javascript:window.returnValue='"+destinationUrl+"';window.close();\" name=\"close\"/>\n");
       buf.append("<script type=\"text/javascript\">\n");
       buf.append("<!--\n");
      
      buf.append("function find(param) \n");
      buf.append("{\n");
      buf.append(" for(var k=0;k<window.document.all.length;k++)\n");
      buf.append(" {\n");
      buf.append("  if(window.document.all(k).name!=\"\"&&window.document.all(k).value!=null)\n");
      buf.append("   {\n");
      buf.append("    var name=window.document.all(k).name;\n");
      buf.append("       if(name.lastIndexOf(param,name.length)==name.length-param.length&&name.length-param.length!=-1)\n");
      buf.append("     {\n");
      buf.append("     var rtn=window.document.all(k).value;\n");
      buf.append("     }\n");
      buf.append("   }\n");
      buf.append(" }\n");
      buf.append("return rtn;\n");
      buf.append("}\n");
      
      buf.append("function rtn() \n");
      buf.append("{\n");
      buf.append("var rtn = new Object();\n");
      String[] params=param.split(",");
       for(int i=0;i<params.length;i++)
       {
        buf.append("rtn."+params[i]+"=find('"+params[i]+"');\n");
        //buf.append("rtn."+params[i]+"='"+new String(paramValues[i])+"';\n");
        //buf.append("rtn."+new String(params[i].getBytes("ISO8859_1"),"GB2312")+";\n");
       }
    
       buf.append("window.returnValue = rtn;\n");
       buf.append("window.close();");
       buf.append("}\n");
       buf.append("//-->\n");
      
       buf.append("</script>\n");
       buf.append("<body onload=\"rtn()\">");
       out.print(buf.toString());
       out.flush();
       //ctx.responseComplete();
}
posted @ 2006-08-21 11:42  泡面 @ 幸福  阅读(490)  评论(0)    收藏  举报