简体   繁体   English

图片的返回地址或错误(如果不存在)

[英]Return address of image or error if it doesn't exist

EDIT:I know my problem is the fact that I can't check if the image is good until it has time to load. 编辑:我知道我的问题是,直到有时间加载,我才能检查图像是否良好。 I'm trying to check width and return after it or the src changes (using onerror to set an src.) I'm always getting stuck with a race condition though, and it errors out long before the height and width or src change. 我试图检查宽度并在其更改或src更改后返回(使用onerror设置src。)尽管如此,我始终陷入竞争状况,并且在高度和宽度或src更改之前很久就出错了。 When I reload, the image is cached and it works fine. 当我重新加载时,图像被缓存,并且工作正常。 I don't know how to avoid this. 我不知道如何避免这种情况。 Here is my current code (also not working, it loops until firefox complains.) I'm setting the return of this function to a variable. 这是我当前的代码(也无法正常工作,直到firefox抱怨之前,它一直循环。)我正在将此函数的返回值设置为变量。 There are some writes in there I'm using to see how far it gets, and it stops in the while loop. 我正在那里写一些内容,以了解它能到达多远,并在while循环中停止。 I tried using getTimeout('tstit = chkload("thisisatest",tinypth);',250);, but that didn't work either. 我尝试使用getTimeout('tstit = chkload(“ thisisatest”,tinypth);',250);,但这也不起作用。 I wish I could force this to load in order... 我希望我可以强制按顺序加载...

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;
}

I need to test for an image, and return error if it is non-existent. 我需要测试图像,如果不存在则返回错误。 The code below works fine on my server: 下面的代码在我的服务器上工作正常:

/*Checks if the image at /tehname/tehname-c.jpg exists in either of the paths
outputs it's address if it does, and "error" if not.*/
function buildimg(tehname,index,pth1,pth2)
{
var myimg = pth1+tehname+"/"+tehname+"-"+index+".jpg";
$.ajax({
url:myimg,
type:'HEAD',
error: function()
{
    myimg=pth2+tehname+"/"+tehname+"-"+index+".jpg";     
    $.ajax({
        url:myimg,
        type:'HEAD',
        error: function()
            {
            myimg="error";
            return;
        },
    });     
    return;
},
});
return myimg;
}

Unfortunately, I'm trying to do this on a messed up system my work uses. 不幸的是,我试图在我的工作使用的混乱系统上做到这一点。 We do have jquery, but the system stores user files on a separate server from code, so ajax won't work. 我们确实有jquery,但是系统将用户文件与代码存储在单独的服务器上,因此ajax无法正常工作。 This will eventually be in a .js file, I hope. 我希望这最终将在.js文件中。

Now I've got code starting with: 现在,我的代码开始于:

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

I tried to have the function load the image, and check its width, but I always get 0, since I can't pre-load the images without knowing how many they are (and I don't want to have some outrageously high number of requests with most being errors.) For some reason (at least on ff4, as that's what my goal is to get working first) tehimage.complete always returns false. 我试图让该函数加载图像并检查其宽度,但是我总是得到0,因为我无法在不知道图像有多少的情况下预加载图像(而且我不希望有高得离谱的数字由于某些原因(至少在ff4上,因为这是我的目标是首先开始工作),tehimage.complete始终返回false。 I've tried using onerror and onload by a global variable, and a few other methods. 我尝试通过全局变量和其他一些方法使用onerror和onload。 I must admit though, I'm not very versed in javascript, so my callbacks may not have worked. 不过,我必须承认,我不太精通javascript,因此我的回调可能无法正常工作。

Please help, I'm getting desperate! 请帮忙,我快要死了!

Thanks, Truk 谢谢,特鲁克

Do you hold the 'power to execute php code' on that user files server? 您是否在该用户文件服务器上拥有“执行php代码的权力”? If so, check them in php and output to images.js in that server, than in your html, first load images.js, then your usual javascript. 如果是这样,请在php中检查它们并将其输出到该服务器(而不是html)中的images.js,首先加载images.js,然后再加载常规的javascript。 By the way, jQuery had memory leaks in ajax for ears, don't know about 1.6, but 1.5 definately had those. 顺便说一句,jQuery的Ajax耳朵内存泄漏,不知道1.6,但是1.5肯定有泄漏。 if you wan't to use ajax to get data from server to javascript - use plain javascript: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp 如果您不想使用Ajax将数据从服务器获取到javascript,请使用纯javascript: http : //www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

Edit: Definately worth checking out: http://fettig.net/weblog/2005/11/28/how-to-make-xmlhttprequest-connections-to-another-server-in-your-domain/ 编辑:绝对值得签出: http : //fettig.net/weblog/2005/11/28/how-to-make-xmlhttprequest-connections-to-another-server-in-your-domain/

Since I'm getting no answers, I'm closing the question. 由于我没有任何答案,因此我关闭了问题。 What I ended up doing was a modification to make the image change class in the onload, have the image onerror delete it, and have the rest of the code run in the window onload. 我最后要做的是进行修改,以使onload中的图像更改类,使image onerror删除它,以及将其余代码在window onload中运行。 Unfortunately, this means the 404s aren't caught before it tries to load more images, so I'm forced to limit the max number of images that can be used (changeable in the function call,) and the images that aren't there just waste a little time. 不幸的是,这意味着在尝试加载更多图像之前未捕获404,因此我不得不限制可以使用的最大图像数量(在函数调用中可更改)和不存在的图像。只是浪费一点时间。

See the final result here. 此处查看最终结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM