2010-01-07 64 views
1

我有一個相當簡單的頁面,它在彈出窗口中顯示圖像。 onLoad事件只需要查詢字符串,使用該值填充圖像標記,然後調整圖像和窗口的大小以匹配。JavaScript只在頁面重新加載時起作用

出於某種原因,這隻適用於頁面重新加載/刷新時。或者,一旦圖像被顯示一次,即使緩存關閉,它也將從此工作。但是,無論何時第一次查看圖像,頁面都會顯示爲空白,並且不會引發錯誤。

有什麼建議嗎?

感謝

<!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'); 
} 
+0

你剛剛從你的硬盤上運行這個?你從網絡服務器上試過了嗎? – 2010-01-07 17:11:03

+0

還沒有嘗試從網絡服務器。這是針對CHM幫助應用程序的,因此它將始終從硬盤驅動器運行。 – Brad 2010-01-07 17:46:48

+0

嘗試在'.hta'文件中運行它。 – 2010-01-07 18:50:38

回答

1

動態撰寫IMG標籤頁面加載過程中使用JavaScript,在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 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> 
0

您需要首先設置SRC的<img>的東西。如果您不想顯示任何內容,只需使用1x1像素透明GIF即可。

+0

在透明的gif中添加了HTML,讓我開心,但仍然和以前一樣。圖像在第一次加載頁面時不顯示。只會在重新加載時顯示。謝謝。 – Brad 2010-01-07 17:45:07

0

你試過綁定的getImage功能窗口的onload事件而不是對身體的onload?

相關問題