简体   繁体   中英

check if folder exists in the root jquery

I'm trying to load an image to a div background using the following file structure in the root.

WebContent --
            |
            zharaimages --
                         |
                         [ItemID] --
                                   |
                                   Image.jpg

This is done by jQuery and the file structure is inside the root. The ItemID folder is dynamic and I have to check whether the path exists using jQuery and if the path is not valid, I should go to a default path to fetch the default image. How can I check the path is valid using jQuery. I'm hoping to this can be done without an ajax call.

Can any one help me on a tutorial or an API I can use for this!

UPDATE

The files are on the server. The concept I have is that I have 100s of item elements & I want to load an image for each item element. The images are saved in the server ( a local host ) and the folder hierarchy is divided using the item ID as shown. What I want to do is check whether the image file exists before appending it to the background of the item element div. Is this possible. This is a web application developed using spring.

In simple way you cannot. It is because JavaScript cannot access folders on server side. The only way you could try to check is to invoke $.get in which you pass url to image and handle error if image does not exist. You cannot try to get only folder because if folder listing is disabled you will always get error

you can bind error handler on your image tag, and if error receive you can load your default image.

$('#imgElementID').error(function() {
   $(this).attr('src', 'images/DEFAULT.JPG');
});

without hitting URL you cannot get to know if image exist or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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