最近做一个效果:
一个页面有n条记录,点击一个小图标,运行后台程序查出数据,将数据显示为图,并在弹出的小窗口显示。弹出小窗口的时候要锁定父窗口,关闭解锁父窗口。
开始想使用模式窗口showModalDialog,但是这个对于ie支持,ff、google不支持(弹出来不会锁定父窗口)
判断浏览器,加入使用widow.open也不能解决。
最后只能是使用div的遮盖层了。具体说下:
1.jsp页面:
<a href="javascript:void(0)" onclick="configwin('<s:property value="type.id" />');"><img src="../img/statistics.gif" border="0" style="margin-left:10px"/></a>
点击图片会调用函数configwin,使用struts2的标签传值进去。为了避免跳转:href="javascript:void(0)"
使用ajax的框架prototype.js来发送请求获得相应的数据库数据。
为什么不使用直接的action的url呢?===action的最后都是转向一个页面,如果直接使用就会跳转到页面去。这点很重要
所以:
<script type="text/javascript"? src="../js/prototype.js"></script>
???
??????? <script type="text/javascript">
??????????? function configwin(id) {
??????????????? var tmp = Math.random().toString();
??????????????? var url = "proto_chart.action?tmp="+tmp+"&typeId="+id;
??????????????? new Ajax.Request(url, {
??????????????????? method:'post',
??????????????????? asynchronous:true ,
??????????????????? onSuccess:function(req){
??????????????????????? sAlert("chart/chart"+id+".jpg");
??????????????????? }
??????????????? });
??????????? }
红字部分是:当浏览器向同一个url发请求,浏览器就会记忆下来,当接到相同请求的时候就不发请求了,直接用以往数据来返回,所以,添加了随机数,保证每次url都不同,Math是js内部对象,不是java的,直接使用即可,不需要引用。
蓝色部分是:封装了遮盖层的div的js。
内部流程说下:
请求给action,action从数据库提取值出来调用Jfreechart,并保存为jpg图片,命名为chartid。jpg
action返回是Action.none(空)
在封装了的js中将url作为img的src赋值,并弹出div来。
js如下(注:此代码是网上搜罗来加工了一小下的,感谢作者的无私贡献)
function sAlert(str){??
var msgw,msgh,bordercolor;??
msgw=300;
msgh=300;??
titleheight=25;???
bordercolor="#c51100";
titlecolor="#c51100";
var sWidth,sHeight;??
sWidth=screen.width;??
sHeight=screen.height;??
var bgObj=document.createElement("div");??
bgObj.setAttribute('id','bgDiv');??
bgObj.style.position="absolute";??
bgObj.style.top="0";??
bgObj.style.background="#cccccc";??
bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";??
bgObj.style.opacity="0.6";??
bgObj.style.left="0";??
bgObj.style.width=sWidth + "px";??
bgObj.style.height=sHeight + "px";??
bgObj.style.zIndex = "10000";??
document.body.appendChild(bgObj);??
var msgObj=document.createElement("div");??
msgObj.setAttribute("id","msgDiv");??
msgObj.setAttribute("align","center");??
msgObj.style.background="white";??
msgObj.style.border="1px solid " + bordercolor;??
msgObj.style.position = "absolute";??
msgObj.style.left = "50%";??
msgObj.style.top = "50%";??
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";??
msgObj.style.marginLeft = "-225px" ;??
msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";??
msgObj.style.width = msgw + "px";??
msgObj.style.height =msgh + "px";??
msgObj.style.textAlign = "center";??
msgObj.style.lineHeight ="25px";??
msgObj.style.zIndex = "10001";??
?
var title=document.createElement("h4");??
title.setAttribute("id","msgTitle");??
title.setAttribute("align","right");??
title.style.margin="0";??
title.style.padding="3px";??
title.style.background=bordercolor;??
title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";??
title.style.opacity="0.75";??
title.style.border="1px solid " + bordercolor;??
title.style.height="18px";??
title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";??
title.style.color="white";??
title.style.cursor="pointer";??
title.innerHTML="close";??
title.onclick=function(){??
document.body.removeChild(bgObj);??
document.getElementById("msgDiv").removeChild(title);??
document.body.removeChild(msgObj);??
};?
document.body.appendChild(msgObj);??
document.getElementById("msgDiv").appendChild(title);
var img=document.createElement("img");??
img.style.margin="1em 0";
img.style.width = msgw-50 + "px";??
img.style.height =msgh -50+ "px";
img.setAttribute("id","imgTxt");??
img.src=str;??
document.getElementById("msgDiv").appendChild(img);????
}?