2011-05-11 29 views
0

編輯:我知道我的問題是,我不能檢查圖像是否好,直到它有時間加載。我試圖檢查寬度,並在它或src更改之後返回(使用onerror設置src)。雖然我總是遇到競爭條件,並且在高度和寬度或src更改之前出錯。當我重新加載時,圖像被緩存並且工作正常。我不知道如何避免這種情況。這裏是我當前的代碼(也不工作,它會循環,直到firefox抱怨。)我設置這個函數返回到一個變量。有一些寫在我用來看它有多遠,它停在while循環。我嘗試過使用getTimeout('tstit = chkload(「thisisatest」,tinypth);',250);但那也沒用。我希望我能迫使這個以加載...圖像或錯誤,如果它不存在的返回地址

function buildimg(tehname,myc,pth1,pth2,tinypth) 
{ 
var myimg = pth1+tehname+"/"+tehname+"-"+myc+".jpg"; 
buildtest("thisisatest",myimg,tinypth); 
var testimg = document.getElementById("thisisatest"); 
var tstit = chkload("thisisatest",tinypth); 
while(tstit == false) { 
     document.write(tstit); 
     tstit = chkload("thisisatest",tinypth); 
    } 
alert(testimg.src+'-'+testimg.width+'-'+testimg.height); 
if((testimage.width > 20) && (testimage.height > 20)) { 
     return myimg; 
    } 
else if(typeof pth2 == "undefined") { 
     myimg = "error"; 
     return myimg; 
    } 
else { 
     myimg = buildimg(tehname,myc,pth2); 
     return myimg; 
    } 

document.write('No return error in buildimg.'); 
return "error"; 
} 

/*Builds a hidden img tag for testing images*/ 
function buildtest(itsid,itssrc,tinypath) { 

if(document.getElementById(itsid)) { 
    var imgobj = document.getElementById(itsid); 
    imgobj.remove(); 
    } 
document.write('<img id="'+itsid+'" style="display: none;" src="'+itssrc+'" onerror="swaponerr(\''+itsid+'\',\''+tinypath+'\')" />'); 
} 

/*Swaps the image to a small picture, so we can detect if it worked*/ 
function swaponerr(tagid, tinypath) { 
    var theimg = document.getElementById(tagid); 
    theimg.onerror = ''; 
    theimg.src = tinypath; 
} 

/*Recurses to return when the image is loaded*/ 
function chkload(loadid,tinychk) { 
var tehobj = document.getElementById(loadid); 
document.write(tehobj.width+'x'+tehobj.height+'x'+tehobj.src); 
if((tehobj.naturalWidth > 20) && (tehobj.naturalHeight > 20)) { 
    return true; 
    } 
if(tehobj.src == tinychk) { 
    return true; 
    } 
return false; 
} 

我需要測試的圖像,如果不存在返回錯誤。下面的代碼工作正常,我的服務器上:

不幸的是,我試圖做這搞砸系統我的工作使用上。我們有jquery,但系統將用戶文件存儲在獨立服務器上的代碼中,所以ajax將無法工作。我希望這將最終以.js文件形式存在。

現在我已經得到了代碼開頭:

function buildimg(tehname,myc,pth1,pth2) 
{ 
    var myimg = pth1+tehname+"/"+tehname+"-"+myc+".jpg"; 
    var tehimage = new Image(); 
    tehimg.src = myimg; 

我試圖具有的功能加載圖像,並檢查其寬度,但我總是得到0,因爲我不能預先加載圖片不知道它們有多少(我不希望有大量錯誤的請求數量過高)。出於某種原因(至少在ff4上,因爲這是我的首要目標)tehimage。完成總是返回false。我已經嘗試使用onerror和onload由一個全局變量,和一些其他方法。我必須承認,雖然我不是很熟悉JavaScript,所以我的回調可能不起作用。

請幫忙,我越來越絕望!

謝謝,Truk

+0

爲什麼ajax無法正常工作?我不明白 – ariel 2011-05-11 06:40:18

+0

由於安全問題,Ajax不會執行跨服務器請求。它可以在我自己的網站上正常運行,所有內容都位於一臺服務器上。 – Truk 2011-05-11 12:30:11

回答

0

由於我沒有得到答案,我關閉題。我最終做的是一個修改,使onload中的圖像更改類,讓圖像onrorror將其刪除,並讓其餘的代碼在窗口onload中運行。不幸的是,這意味着404s在嘗試加載更多圖像之前未被捕獲,所以我不得不限制可以使用的圖像的最大數量(在函數調用中可更改)以及不存在的圖像只是浪費一點時間。

查看最終結果here.

0

您是否擁有在該用戶文件服務器上執行php代碼的權力?如果是這樣,請在php中檢查它們並輸出到該服務器中的images.js,而不是在您的html中,首先加載images.js,然後是通常的JavaScript。 順便說一下,jQuery的耳朵內存泄漏ajax,不知道約1.6,但1.5肯定有這些。如果你wan't使用Ajax從服務器獲取數據的javascript - 使用普通的JavaScript:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

編輯:絕對值得檢查出:http://fettig.net/weblog/2005/11/28/how-to-make-xmlhttprequest-connections-to-another-server-in-your-domain/

+0

不,它運行在ASP上,我甚至不能使用它。否則,我現在已經完成了。這不是我不想使用ajax,我不能。它不會跨服務器工作 – Truk 2011-05-11 12:22:11

+0

不幸的是,這需要比我有更多的圖像服務器的可訪問性。我所能做的只是上傳它們,我不能上傳html/js或任何有用的東西......關於準備放棄並告訴老闆太壞,DX – Truk 2011-05-13 23:29:21

相關問題