2011-05-11 72 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

相关问题