打开指定大小的新窗口

使用JavaScript打开一个弹出窗口,可以使用window对象的open()方法或showModalDialog()方法完成。
在这里插入图片描述在这里插入图片描述
JavaBean类

public class ShowWindow {
	private String url;					//打开窗口的链接地址
	private String openWindowStr="";	//用于保存打开窗口的JavaSript代码
	private int width;					//打开窗口的宽度
	private int height;					//打开窗口的高度
	private String functionName;		//打开窗口的JavaScript函数名
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getOpenWindowStr() {
		StringBuffer sb = new StringBuffer(openWindowStr);
		sb.append("<script language='javascript'>");
		sb.append("\r\n\t");							//添加换行缩进
		sb.append("function "+this.functionName+"(){");	//添加函数名
		sb.append("\r\n\t\t");
		//打开一个窗口时,返回一个window类型的对象returnObj,可以根据此对象来调整窗口的位置
		sb.append("var returnObj = window.open('"+this.url+"','window','width="+this.width+"px,height="+this.height+"px');");		
		sb.append("\r\n\t\t");
		sb.append("var x=(screen.width-"+width+")/2;");	//screen对象表示屏幕,此处设置相对于屏幕的x坐标
		sb.append("\r\n\t\t");
	    sb.append("var y=(screen.height-"+height+")/2;");//此处设置相对于屏幕的y坐标
	    sb.append("\r\n\t\t");
	    sb.append("returnObj.moveTo(x,y);");			//调用moveTo()方法改变窗口位置
		sb.append("\r\n\t}");
		sb.append("\r\n");
		sb.append("</script>");
		return sb.toString();
	}
	public void setOpenWindowStr(String openWindowStr) {
		this.openWindowStr = openWindowStr;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public String getFunctionName() {
		return functionName;
	}
	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}
	public static void main(String [] args){
		ShowWindow s = new ShowWindow();
		s.setFunctionName("openWindow");
		s.setUrl("index.jsp");
		s.setWidth(500);
		s.setHeight(500);
		System.out.println(s.getOpenWindowStr());
	}
}

OpenCenterWindow类

   public class OpenCenterWindow {
    /****************************************
        *功能:该方法将生成一个字符串,该字符串用于输出自定义的JavaScript函数(该函数用于打开指定大小的新窗口,并居中显示)
    ****************************************/
        public String open(String url,String w,String h,String no){
            String str="<script language='javascript'>function openwin"+no+"(){";
            str=str+"if ("+w+"=='0'){var winhdc=window.open('"+url+"');";
            str=str+"var width=0;var height=0;}else{";
            str=str+"var winhdc=window.open('"+url+"','','width="+w+",height="+h+"');";
            str=str+"var width=(screen.width-"+w+")/2;";
            str=str+"var height=(screen.height-"+h+")/2;}";
            str=str+"winhdc.moveTo(width,height);";
            str=str+"}</script>";
            return str;
        }
    }

index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  
  	<!-- 导入打开窗口的JavaBean类 -->
  	<jsp:useBean id="myWindow" class="com.cn.zj.test.ShowWindow"></jsp:useBean>
  	<!-- 设置打开窗口的JavaScript函数名 -->
	<jsp:setProperty property="functionName" name="myWindow" value="openWindow1"/>
	<!-- 设置打开窗口的url地址 -->
	<jsp:setProperty property="url" name="myWindow" value="window.jsp"/>
	<!-- 设置打开窗口的宽度 -->
	<jsp:setProperty property="width" name="myWindow" value="200"/>
	<!-- 设置打开窗口的高度 -->
	<jsp:setProperty property="height" name="myWindow" value="100"/>
	<!-- 获得打开窗口的JavaScript函数字符串 -->
	<jsp:getProperty property="openWindowStr"  name="myWindow" />
    <form action="window.jsp" method="post">
    	<input  type="button" value="打开窗口" onclick="openWindow1()"/>
    </form>
  </body>
</html>

window.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>打开窗口</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
		body{
			font-size: 15px;
			color:green;
			font-family: 隶书;
			background-color: pink;
			
		}
	</style>
  </head>
  
  <body>
  	你好!我是被打开的新窗口!宽度200px,高度100px,居中显示。
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44234912/article/details/88398695