【问题标题】:JavaScript only works when page is reloadedJavaScript 仅在页面重新加载时有效
【发布时间】:2010-01-07 16:55:56
【问题描述】:

我有一个相当简单的页面,它在弹出窗口中显示图像。 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');
}

【问题讨论】:

  • 您只是从硬盘上运行它吗?您是否在网络服务器上尝试过?
  • 没有从网络服务器尝试过。这是一个 CHM 帮助应用程序,所以它总是从硬盘驱动器运行。
  • 尝试在.hta 文件中运行它。

标签: javascript image reload


【解决方案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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 添加了透明 gif 以使 HTML 快乐,但我仍然遇到与以前相同的问题。第一次加载页面时图像不显示。只会在重新加载时显示。谢谢。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      相关资源
      最近更新 更多