2010-11-11 17 views
0

這是我從網絡獲取圖像寬度的代碼。 它很好用。使用JavaScript獲取圖像寬度有問題?

<script type="text/javascript"> 
function CreateDelegate(contextObject, delegateMethod) 
{ 
     return function() 
      { 
       return delegateMethod.apply(contextObject, arguments); 
      } 
} 


function imgTesting_onload() 
{ 
    image_width=this.width; 
    alert(image_width);//First Alert 
} 
</script> 
var imgTesting = new Image(); 
imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload); 
imgTesting.src = "${str_applicationPath}/common/images/reports/"+report+".png" 
//Second Alert// 

但我需要的是在第二次// //警報地方訪問IMAGE_WIDTH變量 上述code.I我沒有得到寬度值there.Is什麼工作要做,以 GET值有?

請幫我......... 由於提前

+0

你需要什麼?就像RoToRa說的那樣,這是不可能的,所以也許我們可以幫助你找到解決你最初需求的方法。 – 2010-11-11 15:20:34

回答

-1

可以使用imgTesting.width獲得圖像的寬度。

例如:

變種imgTesting =新的圖像();

imgTesting.src => 「$ {str_applicationPath} /普通/圖像/報告/」 +報告+

//第二類提醒//

警報(imgTesting.width)名 「PNG」;

如下代碼不需要

function CreateDelegate(contextObject, delegateMethod) 
{ 
     return function() 
      { 
       return delegateMethod.apply(contextObject, arguments); 
      } 
} 
function imgTesting_onload() 
{ 
    image_width=this.width; 
    alert(image_width);//First Alert 
} 
imgTesting.onload =CreateDelegate(imgTesting, imgTesting_onload); 
+0

-1恩,是的。圖像加載異步,因此設置'src'之後最有可能不知道寬度。 – RoToRa 2010-11-11 12:16:40

2

那是不可能的。在那一點上,寬度是未知的。首先,這是onload事件的重點。您必須在imgTesting_onload中的「Second alert」中執行所需的代碼。