Javascript 如何检查指定URL中的图像是否已加载
通过ajax调用,我获取了需要用来更改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
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,它通常对回调有很好的支持。