让弹出窗口(网页对话框)自适应图片大小

好长时间没写东西了,唉,瞎忙.
今天上午网站上需要一个弹出窗口,要求是图片多大, 窗口就多大.又是JS的问题,查了又查,终于有点眉目了,就是Image()对象可以解决这个问题.网上有很多例子,但都是弹出的窗口(虽然这篇题目也是写的窗口),而我要的是弹出”网页对话框”,只有一个关闭按钮,没有右键的.
下面这段代码是经过我修改过的,能达到上述的效果,现在贴出来供大家参考:

<script type="text/javascript" language="javascript">

<!--

/*Jena.want 2007/03/30 www.IsHere.cn*/

var imgObj;

function checkImg(theURL,winName){ if (typeof(imgObj) == "object"){ if ((imgObj.width != 0) &#038;&#038; (imgObj.height != 0)) //OpenFullSizeWindow(theURL,winName, ",width=" + (imgObj.width+20) + ",height=" + (imgObj.height+30)); OpenFullSizeWindow(theURL,winName, "dialogWidth:" + (imgObj.width) + "px;dialogHeight:" + (imgObj.height)); else setTimeout("checkImg(\'" + theURL + "\',\'" + winName + "\')", 100); } }

function OpenFullSizeWindow(theURL,winName,features) {

var aNewWin, sBaseCmd; //sBaseCmd = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,"; sBaseCmd = "px;dialogleft:100px;help:no;status:no;scroll:no";

if (features == null || features == ""){ imgObj = new Image();// 创建图像对象 imgObj.src = theURL;// 设置图像源 checkImg(theURL, winName)// 开始获取图像大小 } else{ //aNewWin = window.open(theURL,winName, sBaseCmd + features);// 打开窗口 aNewWin = showModalDialog(theURL,winName,features + sBaseCmd); //aNewWin.focus();// 聚焦窗口,这个地方在弹出网页对话框的时候不能使用. } }

//--></script>

然后这样调用:

<a href="http://www.ishere.cn/styles/blogLogo88_31.gif" onclick="OpenFullSizeWindow(this.href,'','');return false"><img src="http://www.ishere.cn/styles/blogLogo.gif" /></a>

Leave a Comment

Your email address will not be published.

*