Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查指定URL中的图像是否已加载_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何检查指定URL中的图像是否已加载

Javascript 如何检查指定URL中的图像是否已加载,javascript,jquery,ajax,Javascript,Jquery,Ajax,通过ajax调用,我获取了需要用来更改img的src的url。然而,这需要时间。同时,我需要展示一下装载情况。如何检查$(“#img#”).attr(“src”,“http:\\fsomething”)是否已在页面上加载图像?我将在AJAX调用之前添加调用以显示加载图像: $('#ajaxLoadingContainer').html('<img src="secure_content/images/ajax-loader-small.gif">'); $.ajax({ u

通过ajax调用,我获取了需要用来更改
img
src
的url。然而,这需要时间。同时,我需要展示一下装载情况。如何检查
$(“#img#”).attr(“src”,“http:\\fsomething”)
是否已在页面上加载图像?

我将在AJAX调用之前添加调用以显示加载图像:

$('#ajaxLoadingContainer').html('<img src="secure_content/images/ajax-loader-small.gif">');

$.ajax({
    url: 'secure/ajax.php?action=checkSession',
    type: 'GET',
    dataType: 'HTML',
    async: true,
    success: removeAjaxLoader,
});

function removeAjaxLoader() {
    $('#ajaxLoadingContainer').html('');
}
$('#ajaxLoadingContainer').html('';
$.ajax({
url:'secure/ajax.php?action=checkSession',
键入:“GET”,
数据类型:“HTML”,
async:true,
成功:removeAjaxLoader,
});
函数removeAjaxLoader(){
$('#ajaxLoadingContainer').html('';
}

加载新映像后,使用以下代码获取回调事件

var isrc = "http:\\fsomething ";
var image = new Image();
    image.onload=function(){
       //Image load callback
    }
image.src = isrc;
$('body').append(image);
在ajax成功函数中使用此代码。在ajax调用之前显示加载图像,并在图像加载回调函数中隐藏加载图像。

尝试此操作

$.ajax({
url:'http://stackoverflow.com/secure_content/images/ajax-loader-small.gif',
type:'HEAD',
error: function()
{
    //file not exists
},
success: function()
{
    //file exists
}
});

您可以使用javascript Image()实现此目的


var img=新图像();
//在此处显示加载图像
jQuery('#loadingImg').show();
img.onload=函数(){
//图像已加载
//隐藏正在加载的图像
jQuery('#loadingImg').hide();
};
img.src='
image_url
';

您能显示AJAX调用的代码吗?我的建议是在AJAX调用之前添加加载映像,然后在成功时添加AJAX回调以删除加载映像。@Spokey我可能同意,除了这个问题,没有一个答案使用jQuery,它通常对回调有很好的支持。