I'm using an slideshow and I would like check if there any image on a specific folder since I'll make it all dynamically. I开发者_高级运维 saw some questions here about this but can't get it work.
<div class="foo">
<img src="images/[dynamic_folder_name]/1.jpg" alt="" />
<img src="images/[dynamic_folder_name]/2.jpg" alt="" />
</div>
There'll be allot of folders and maybe some could be empty so I wanna check with jquery if there's any images with those names and if not set a default image.
If you have any other ideas for doing this instead of using jquery, you're more than welcome.
Thanks in advance.
JavaScript runs within the clients browser which has no knowledge of your servers folder structure. You'll need to create a server script which supplies the required information to the scripts running in the browser.
You can use script like this.
function testImage(src,callback) {
var testimg = new Image();
testimg.onerror(function(){
callback(false);
});
testimg.onload(function(){
callback(true);
});
testimg.load(src);
}
Then call the function to check if image exeist.
精彩评论