1

我有一个相当简单的页面,在弹出窗口中显示图像。onLoad 事件只需获取查询字符串,使用该值填充图像标记,然后调整图像和窗口的大小以匹配。

出于某种原因,这仅在重新加载/刷新页面时对我有效。或者,一旦图像显示一次,它就会从那时起工作,即使缓存已关闭。但是任何时候第一次查看图像时,页面都会出现空白,不会引发任何错误。

有什么建议么?

谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Image Zoom</title>
    <meta http-equiv="Pragma" content="no-cache" />
    <script type="text/javascript">
        function getImage() {
            //get the query string (everything after the ?)
            var filename = window.location.search.substring(1);
            //find the image control
            var imageWorking = document.getElementById('dynamicImage');
            //assign the image source
            imageWorking.src = '../Images/' + filename;
            //create an image variable
            var newImg = new Image();
            //assign the source to match the page image
            newImg.src = imageWorking.src;
            //get the width and height
            var width = newImg.width;
            var height = newImg.height;
            //set the page image width and height to match the disk image
            imageWorking.width = width;
            imageWorking.height = height;
            //set the window to be just larger than the image
            window.resizeTo(width + 50,height + 50);
        }
    </script>
</head>
<body onload="getImage();">
    <img src="images/spacer.gif" id="dynamicImage" alt="Image Zoom" width="1" height="1" />
</body>
</html>

该页面使用以下函数调用:

function imageZoom(imageName)
{
    window.open('ImageZoom.htm?' + imageName + '','imageZoom','width=400,height=400,menubar=no,toobar=no,scrollbars=yes,resizable=yes');
}
4

3 回答 3

1

在页面加载期间使用 JavaScript 动态编写 IMG 标签,并在加载时调整窗口大小:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Image Zoom</title>
  <meta http-equiv="Pragma" content="no-cache" />
  <script type="text/javascript">
    function sizeWindow() {

      //find the image control
      var img = document.getElementById('dynamicImage');

      //set the window to be just larger than the image
      window.resizeTo(img.width + 50, img.height + 50);
    }
  </script>
</head>
<body onload="sizeWindow();">
  <script type="text/javascript">
    var filename = window.location.search.substring(1);
    document.write("<img src='../Images/" + filename + "' id='dynamicImage' alt='Image Zoom' />");
  </script>
</body>
</html>
于 2010-01-07T18:14:30.567 回答
0

<img>您最初需要将 的 SRC 设置为某个值。如果您不想显示任何内容,只需使用 1x1 像素的透明 GIF。

于 2010-01-07T17:10:45.737 回答
0

您是否尝试将 getImage 函数绑定到窗口的 onload 事件,而不是绑定到正文的 onload?

于 2010-01-07T18:51:03.520 回答